C
C#13mo ago
Salight

How can i sort a data i have in c#

RB - 28 YAŞ- Heartles - 66 M
RB - 20 YAŞ- Jeremy Doku - 86 M
RB - 27 YAŞ- Arıza - 94 M
RB - 25 YAŞ- Bright - 122 M
LB - 22 YAŞ- Griffin - 69 M
LB - 28 YAŞ- Rythia - 108 M
LB - 25 YAŞ- İbo Şayşayro - 73 M
RB - 22 YAŞ- Nakeshi - 79 M
LB - 29 YAŞ- Rage Reshly - 39 M
LB - 26 YAŞ- ba2van - 43 M
LB - 20 YAŞ- Cyg - 48 M
LB - 19 YAŞ- Felix - 52 M
LB - 27 YAŞ- GodOfPosko - 38 M
LB - 23 YAŞ- leqendd - 56 M
RB - 27 YAŞ- Reipsito - 82 M
RB - 28 YAŞ- Türkay - 48 M
LB - 28 YAŞ- Algos - 72 M
LB - 21 YAŞ- Mertuzun1234 - 49 M
LB - 22 YAŞ- Szykowiak - 32 M
LB - 25 YAŞ- Lakaka - 51 M
LB - 23 YAŞ- rexa - 36 M
RB - 21 YAŞ- Kekreyen - 39 M
RB - 30 YAŞ- Bismarck - 49 M
RB - 30 YAŞ- leo's - 52 M
LB - 31 YAŞ- Kralahmet 140M
RB - 24 YAŞ- Emiric 108M
RB - 24 YAŞ- Clark Kent 71M
LB - 21 YAŞ- Salight - 65M
LB - 23 YAŞ- Garrincha - 56M
LB - 18 YAŞ- Mkc - 47M
LB - 26 YAŞ- Misaki - 39M
LB - 28 YAŞ- Nearby - 61M
LB - 35 YAŞ- Casa - 105M
RB - 28 YAŞ- Heartles - 66 M
RB - 20 YAŞ- Jeremy Doku - 86 M
RB - 27 YAŞ- Arıza - 94 M
RB - 25 YAŞ- Bright - 122 M
LB - 22 YAŞ- Griffin - 69 M
LB - 28 YAŞ- Rythia - 108 M
LB - 25 YAŞ- İbo Şayşayro - 73 M
RB - 22 YAŞ- Nakeshi - 79 M
LB - 29 YAŞ- Rage Reshly - 39 M
LB - 26 YAŞ- ba2van - 43 M
LB - 20 YAŞ- Cyg - 48 M
LB - 19 YAŞ- Felix - 52 M
LB - 27 YAŞ- GodOfPosko - 38 M
LB - 23 YAŞ- leqendd - 56 M
RB - 27 YAŞ- Reipsito - 82 M
RB - 28 YAŞ- Türkay - 48 M
LB - 28 YAŞ- Algos - 72 M
LB - 21 YAŞ- Mertuzun1234 - 49 M
LB - 22 YAŞ- Szykowiak - 32 M
LB - 25 YAŞ- Lakaka - 51 M
LB - 23 YAŞ- rexa - 36 M
RB - 21 YAŞ- Kekreyen - 39 M
RB - 30 YAŞ- Bismarck - 49 M
RB - 30 YAŞ- leo's - 52 M
LB - 31 YAŞ- Kralahmet 140M
RB - 24 YAŞ- Emiric 108M
RB - 24 YAŞ- Clark Kent 71M
LB - 21 YAŞ- Salight - 65M
LB - 23 YAŞ- Garrincha - 56M
LB - 18 YAŞ- Mkc - 47M
LB - 26 YAŞ- Misaki - 39M
LB - 28 YAŞ- Nearby - 61M
LB - 35 YAŞ- Casa - 105M
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
Angius
Angius13mo ago
Split by whatever so you only get the segment you want to sort by, and sort by that For example,
MODiX
MODiX13mo ago
Angius
REPL Result: Success
var lst = new[]{
"abc8",
"def7",
"ghijkl3",
"zxcv1",
"uiopqwertys5"
};
lst.OrderBy(s => int.Parse(s[^1].ToString()))
var lst = new[]{
"abc8",
"def7",
"ghijkl3",
"zxcv1",
"uiopqwertys5"
};
lst.OrderBy(s => int.Parse(s[^1].ToString()))
Result: OrderedEnumerable<string, int>
[
"zxcv1",
"ghijkl3",
"uiopqwertys5",
"def7",
"abc8"
]
[
"zxcv1",
"ghijkl3",
"uiopqwertys5",
"def7",
"abc8"
]
Compile: 676.964ms | Execution: 74.098ms | React with ❌ to remove this embed.
Salight
SalightOP13mo ago
wow but i didn't understand the part parse method
MODiX
MODiX13mo ago
Angius
REPL Result: Success
int.Parse("566")
int.Parse("566")
Result: int
566
566
Compile: 470.036ms | Execution: 26.996ms | React with ❌ to remove this embed.
Angius
Angius13mo ago
It turns a string into an int
Salight
SalightOP13mo ago
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
cap5lut
cap5lut13mo ago
s[^1] is short hand for s[s.Length - 1]
Salight
SalightOP13mo ago
:d this code seems too complicated for me now i got one more question where did we define s what is s
cap5lut
cap5lut13mo ago
well, u know that the index starts at 0 right?
Salight
SalightOP13mo ago
ye
cap5lut
cap5lut13mo ago
ah, that s is from the lambda expression
Salight
SalightOP13mo ago
isn't that the lambda, is all about short if else
cap5lut
cap5lut13mo ago
if u mean something like condition ? value1 : value2, thats a ternary operator
Salight
SalightOP13mo ago
oh mb
cap5lut
cap5lut13mo ago
a lambda is a short written method
Salight
SalightOP13mo ago
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?
MODiX
MODiX13mo ago
cap5lut
REPL Result: Success
Func<int, int> function = number => number * 2;
for (int i = 0; i < 3; i++)
{
int result = function(i);
Console.WriteLine(result);
}
Func<int, int> function = number => number * 2;
for (int i = 0; i < 3; i++)
{
int result = function(i);
Console.WriteLine(result);
}
Console Output
0
2
4
0
2
4
Compile: 680.717ms | Execution: 78.199ms | React with ❌ to remove this embed.
Salight
SalightOP13mo ago
so this is lambda right
cap5lut
cap5lut13mo ago
yep
Salight
SalightOP13mo ago
why didn't i see that before huh weird am i tweaking or what
cap5lut
cap5lut13mo ago
well, the number => number * 2 part is the lambda expression
Salight
SalightOP13mo ago
got it
cap5lut
cap5lut13mo ago
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 by
Salight
SalightOP13mo ago
what do you mean by value selector
cap5lut
cap5lut13mo ago
well, imagine this example:
Salight
SalightOP13mo ago
i was thinking like after the M char convert the number to int than sort this value in for loop
cap5lut
cap5lut13mo ago
public record Person(string Name, int Age);

var persons = new Person[]
{
new Person("Max", 35),
new Person("Thomas", 18),
new Person("Sophia", 73)
};
public record Person(string Name, int Age);

var persons = new Person[]
{
new Person("Max", 35),
new Person("Thomas", 18),
new Person("Sophia", 73)
};
if u wanted to sort that array by the Name property, u would have to express that somehow
MODiX
MODiX13mo ago
cap5lut
REPL Result: Success
public record Person(string Name, int Age);

var persons = new Person[]
{
new Person("Max", 35),
new Person("Thomas", 18),
new Person("Sophia", 73)
}
.OrderBy(p => p.Name)
.ToArray();
persons
public record Person(string Name, int Age);

var persons = new Person[]
{
new Person("Max", 35),
new Person("Thomas", 18),
new Person("Sophia", 73)
}
.OrderBy(p => p.Name)
.ToArray();
persons
Result: Person[]
[
{
"name": "Max",
"age": 35
},
{
"name": "Sophia",
"age": 73
},
{
"name": "Thomas",
"age": 18
}
]
[
{
"name": "Max",
"age": 35
},
{
"name": "Sophia",
"age": 73
},
{
"name": "Thomas",
"age": 18
}
]
Compile: 660.481ms | Execution: 106.197ms | React with ❌ to remove this embed.
Salight
SalightOP13mo ago
okay i got this but my main question is how could i change this large data to this
new Person("Max", 35),
new Person("Thomas", 18),
new Person("Sophia", 73)
new Person("Max", 35),
new Person("Thomas", 18),
new Person("Sophia", 73)
like
cap5lut
cap5lut13mo ago
how do u have that data? as file?
Salight
SalightOP13mo ago
like this . exactly the same
cap5lut
cap5lut13mo ago
well, is it one fat string? do u read it from a text file? is it the response of a http request?
Salight
SalightOP13mo ago
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
cap5lut
cap5lut13mo ago
well, if its a file anyway we just read line by line ;p
Salight
SalightOP13mo ago
can't we do something
cap5lut
cap5lut13mo ago
looking at the format it seems like a - delimited string
Salight
SalightOP13mo ago
yes that's what i am talking about can't we cut the
-
-
parts an make that parts an array index
cap5lut
cap5lut13mo ago
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) method
Salight
SalightOP13mo ago
hmm no we have to cut it from M
MODiX
MODiX13mo ago
cap5lut
REPL Result: Success
"hello world".Split(' ')
"hello world".Split(' ')
Result: string[]
[
"hello",
"world"
]
[
"hello",
"world"
]
Compile: 464.279ms | Execution: 28.708ms | React with ❌ to remove this embed.
Salight
SalightOP13mo ago
that would be more correct YES THAT İ IS WHAT I AM TALKİNG ABOUT
MODiX
MODiX13mo ago
cap5lut
REPL Result: Success
"RB - 28 YAŞ- Türkay - 48 M".Split('-')
"RB - 28 YAŞ- Türkay - 48 M".Split('-')
Result: string[]
[
"RB ",
" 28 YAŞ",
" Türkay ",
" 48 M"
]
[
"RB ",
" 28 YAŞ",
" Türkay ",
" 48 M"
]
Compile: 430.946ms | Execution: 41.537ms | React with ❌ to remove this embed.
Salight
SalightOP13mo ago
so let me ask you something if we adjust this method to a string x or something is it going to be array
cap5lut
cap5lut13mo ago
the Split method returns an array, yes
Salight
SalightOP13mo ago
string[] x=data.Split('M'); like this
cap5lut
cap5lut13mo ago
is data the whole file content as one string?
Salight
SalightOP13mo ago
yes i think we solved it with this method you*
cap5lut
cap5lut13mo ago
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
foreach (string line in File.ReadLines("path/to/your/file.txt"))
{
// do something
}
foreach (string line in File.ReadLines("path/to/your/file.txt"))
{
// do something
}
Salight
SalightOP13mo ago
this makes more sense i was going to paste it all this in the IDE
cap5lut
cap5lut13mo ago
oh yeah, im missing a ) at the end, fixed
Salight
SalightOP13mo ago
you helped me a lot mate thank you very much i obey you
cap5lut
cap5lut13mo ago
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)
Salight
SalightOP13mo ago
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
cap5lut
cap5lut13mo ago
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 list
Salight
SalightOP13mo ago
these are bit of hard for me i am a newbie but i am considering all of them in my mind
cap5lut
cap5lut13mo ago
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 has
Salight
SalightOP13mo ago
i just learnt interface there seems to be long road i guess but i will learn eventually how long have you been coding ?
cap5lut
cap5lut13mo ago
u should have learned classes before interfaces i started learning by myself ~2000
Salight
SalightOP13mo ago
2000 hours you meant by that right
cap5lut
cap5lut13mo ago
no, the year 2000
Salight
SalightOP13mo ago
wow
cap5lut
cap5lut13mo ago
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 😂
Salight
SalightOP13mo ago
same :d anyways man goodnight thanks a lot
Omnissiah
Omnissiah13mo ago
you can extract a key for the value and sort in linq or you can add that key/value to a sorted collection
Want results from more Discord servers?
Add your server