How can i sort a data i have in c#
txt
the data is this and i want to learn how to sort this data in string array after that i want to sort this Million values by int so i can sort by value i hope i could explain myself
64 Replies
Split by whatever so you only get the segment you want to sort by, and sort by that
For example,
Angius
REPL Result: Success
Result: OrderedEnumerable<string, int>
Compile: 676.964ms | Execution: 74.098ms | React with ❌ to remove this embed.
wow
but
i didn't understand the part parse method
Angius
REPL Result: Success
Result: int
Compile: 470.036ms | Execution: 26.996ms | React with ❌ to remove this embed.
It turns a string into an int
s[^1]
what is this means
and i got one more question so as you can see it is a long data and i have more than that do i have to put quotes and comma for every single array element or can we make this easier to put that all by itself in array
s[^1]
is short hand for s[s.Length - 1]
:d
this code seems too complicated for me
now i got one more question
where did we define s what is s
well, u know that the index starts at 0 right?
ye
ah, that
s
is from the lambda expressionisn't that the lambda, is all about short if else
if u mean something like
condition ? value1 : value2
, thats a ternary operatoroh
mb
a lambda is a short written method
so let me ask you something the data up over here that i pasted it. Could we order by value? just with the order.by method?
cap5lut
REPL Result: Success
Console Output
Compile: 680.717ms | Execution: 78.199ms | React with ❌ to remove this embed.
so this is lambda right
yep
why didn't i see that before huh weird
am i tweaking or what
well, the
number => number * 2
part is the lambda expressiongot it
function
is a variable of the delegate type Func<T, R>
anyway, OrderBy
is a method that wants a "value selector" from u,
it runs this selector for each entry to get values to sort bywhat do you mean by value selector
well, imagine this example:
i was thinking like after the M char convert the number to int than sort this value in for loop
if u wanted to sort that array by the
Name
property, u would have to express that somehowcap5lut
REPL Result: Success
Result: Person[]
Compile: 660.481ms | Execution: 106.197ms | React with ❌ to remove this embed.
okay i got this but my main question is how could i change this large data to this
like
how do u have that data?
as file?
like this
.
exactly
the same
well, is it one fat string?
do u read it from a text file?
is it the response of a http request?
it is a text file
it is not that fat
but i thought if i have to do this i don't want to do it manually
well, if its a file anyway we just read line by line ;p
can't we do something
looking at the format
it seems like a
-
delimited stringyes that's what i am talking about can't we cut the parts an make that parts an array index
so
RB - 28 YAŞ- Türkay - 48 M
would be split into
RB
,
28 YAŞ
,
Türkay
and 48 M
there is the string.Split(char)
methodhmm no we have to cut it from M
cap5lut
REPL Result: Success
Result: string[]
Compile: 464.279ms | Execution: 28.708ms | React with ❌ to remove this embed.
that would be more correct
YES
THAT İ
IS WHAT I AM TALKİNG ABOUT
cap5lut
REPL Result: Success
Result: string[]
Compile: 430.946ms | Execution: 41.537ms | React with ❌ to remove this embed.
so let me ask you something
if we adjust this method to a string x or something
is it going to be array
the
Split
method returns an array, yesstring[] x=data.Split('M');
like this
is
data
the whole file content as one string?yes
i think we solved it with this method
you*
well, then the first step would be to split the content at its new lines
or simply read the file line by line
eg with this u read the file line by line
this makes more sense
i was going to paste it all this in the IDE
oh yeah, im missing a
)
at the end, fixedyou helped me a lot mate
thank you very much
i obey you
basically u need 3 things to get the data:
- a class/record that can hold the information for one entry
- a parse method that takes one line as string and returns an instance
- a method that takes a file path and returns a list or array of all the instances it parsed
with the first i cant help ya, because i have no clue what that data is about (besides that it has something to do with countries or regions)
i was thinking about the class but i didn't know the txt methods but thanks to you i learnt that
thank you a lot again
the parse method will be a bit annoying unless u wanna/can use regular expressions
and the last method is basically the
foreach
loop, which calls the parse method and stuffs the result into a listthese are bit of hard for me i am a newbie
but i am considering all of them
in my mind
hmm i guess u didnt learn about
Regex
yet, they are also quite cryptic, so not that for now
anyway, all that starts with thinking about the properties an entry has and to create a class or record from that
that will also mean u need to think about which data type each of these properties hasi just learnt interface there seems to be long road i guess but i will learn eventually how long have you been coding ?
u should have learned classes before interfaces
i started learning by myself ~2000
2000 hours
you meant by that right
no, the year 2000
wow
but that was in other languages.
first started messing with c# in 2017, but really actively used it 2021
(basically i didnt give a shit about c# before it started being platform independent)
but i gotta hit the sack for tonight, i have to wake up in 5h 😂
same :d
anyways man goodnight
thanks a lot
you can extract a key for the value and sort in linq
or you can add that key/value to a sorted collection