'Please help me to sort this kinda lists
first of all, im still kinda new to coding and have not that much knowledge ^^ So here is my problem, I need to create a class/method that can sort a list, that look like this:
.Add("1");
.Add("1.1");
.Add("1.2");
.Add("1.1.1-1");
.Add("1.1.1-1usa");
.Add("7.2");
.Add("8.");
.Add("9.");
.Add("10.1)
the list should be ascend(?) meaning, it starts with the lowest number going up to the highest.
I tried to make char arrays out of them and then compare each char with an if-statement. some mor informations: 1.1.1 > 1.1 and as you can see, there are also letters inside. If someone has an idea I would be very thankful for every tip :)
NOTE: if I would sort it like list.Sort();, 10.1 wouldnt come right after 9, instead its bewtween 1.1 and 2.1 for example
Solution 1:[1]
I have used basic alphabetic sorting and this is the result:
1
1.1
1.1.1-1
1.1.1-1usa
1.2
7.2
8.
9.
As you can see:
1 < 1.1 < 1.1.1-1* < 1.2 < 7.2 < 8. < 9.
// 1.1.1-1* means:
// anything, starting with 1.1.1-1
I believe we can agree on that. As far as the letters are concerned:
1.1.1-1 < 1.1.1-1usa
Also this seems logical, because nothing comes before something.
So it looks like basic alphabetic sorting does the trick, or do you disagree?
Solution 2:[2]
Again a big thanks to everyone. This is the solution i decided to take advantage of:
Numbers= Numbers.OrderBy(v => string.Concat(v.Split('.').Select(x => x.PadLeft(5, '0')))).ToList();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dominique |
| Solution 2 | 247Munchies |
