✅ 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
23 Replies
Angius
REPL Result: Success
Result: List<int>
Compile: 306.914ms | Execution: 29.091ms | React with ❌ to remove this embed.
Works just fine
Angius
REPL Result: Success
Result: List<int>
Compile: 300.684ms | Execution: 29.336ms | React with ❌ to remove this embed.
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
definitely strings being sorted
@Angius
cmp.Clear();
Console.WriteLine(s);
check = s.Split(' ').ToList();
foreach(string z in cmp) { Console.WriteLine(z); }
So, yeah, you're sorting strings
Assuming you sort that
check
Or that cmp
ah how can i parse into ints first
cmp and check is the same thing at this part of the code
int.TryParse()
for example
Or int.Parse()
list? or each string in the cmp list
Each string
is that the most efficient way ://
Easily doable with
theStrings.Select(int.Parse)
but how will i be able to store the ints in the string lists
You can convert them back into strings
after sorting?
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, thoughwould intList.Sort() work as intended, id prefer the lists to be in anyway
Once you have a
List<int>
, then yeah, just .Sort()
ing it will sort it numericallyalright, thanks for the insight. we can close this ❤️
$close
If you have no further questions, please use /close to mark the forum thread as answered