C
C#2mo ago
AyaMaya

✅ List.Sort()?

I am using a list named cmp to compare another list if it is equal to a sorted version of the same list. I am using cmp.Sort(). From what i have seen in the console.writeline... that it does not work at all on the top is the list, on the bottom is each value in cmp, which is clearly not sorted from least to greatest. pls help
No description
23 Replies
MODiX
MODiX2mo ago
Angius
REPL Result: Success
List<int> lst = [ 1, 4, 8, 10, 12, 14, 17 ];
lst.Sort();

lst
List<int> lst = [ 1, 4, 8, 10, 12, 14, 17 ];
lst.Sort();

lst
Result: List<int>
[
1,
4,
8,
10,
12,
14,
17
]
[
1,
4,
8,
10,
12,
14,
17
]
Compile: 306.914ms | Execution: 29.091ms | React with ❌ to remove this embed.
Angius
Angius2mo ago
Works just fine
MODiX
MODiX2mo ago
Angius
REPL Result: Success
List<int> lst = [ 8, 14, 27, 10, 12, 4, 3 ];
lst.Sort();

lst
List<int> lst = [ 8, 14, 27, 10, 12, 4, 3 ];
lst.Sort();

lst
Result: List<int>
[
3,
4,
8,
10,
12,
14,
27
]
[
3,
4,
8,
10,
12,
14,
27
]
Compile: 300.684ms | Execution: 29.336ms | React with ❌ to remove this embed.
Angius
Angius2mo ago
Yeah, sorts just fine What does your code look like? Did you use a lexical comparer somehow? Or you're sorting strings, not numbers, in the first place? Or you could just vanish off the face of the Earth two minutes after dropping your question Sure, that also works
ero
ero2mo ago
definitely strings being sorted
AyaMaya
AyaMayaOP2mo ago
No description
No description
AyaMaya
AyaMayaOP2mo ago
@Angius cmp.Clear(); Console.WriteLine(s); check = s.Split(' ').ToList(); foreach(string z in cmp) { Console.WriteLine(z); }
Angius
Angius2mo ago
So, yeah, you're sorting strings Assuming you sort that check Or that cmp
AyaMaya
AyaMayaOP2mo ago
ah how can i parse into ints first cmp and check is the same thing at this part of the code
Angius
Angius2mo ago
int.TryParse() for example Or int.Parse()
AyaMaya
AyaMayaOP2mo ago
list? or each string in the cmp list
Angius
Angius2mo ago
Each string
AyaMaya
AyaMayaOP2mo ago
is that the most efficient way ://
Angius
Angius2mo ago
Easily doable with theStrings.Select(int.Parse)
AyaMaya
AyaMayaOP2mo ago
but how will i be able to store the ints in the string lists
Angius
Angius2mo ago
You can convert them back into strings
AyaMaya
AyaMayaOP2mo ago
after sorting?
Angius
Angius2mo ago
ye Or you can sort them by their integer version with .OrderBy() for example theStrings.OrderBy(int.Parse) Will sort the strings by their integer version, and still gives yoiu strings It does not sort in place, though
AyaMaya
AyaMayaOP2mo ago
would intList.Sort() work as intended, id prefer the lists to be in anyway
Angius
Angius2mo ago
Once you have a List<int>, then yeah, just .Sort()ing it will sort it numerically
AyaMaya
AyaMayaOP2mo ago
alright, thanks for the insight. we can close this ❤️
Angius
Angius2mo ago
$close
MODiX
MODiX2mo ago
If you have no further questions, please use /close to mark the forum thread as answered

Did you find this page helpful?