I have CSV data like this:
Number;Version
1;AA.1
1;A01.1
1;A01.2
1;Z.7
Here, I need to sort the Version column descending like following:
Number;Version
1;AA.1
1;Z.7
1;A01.2
1;A01.1
So if you see, the Z.7 entry should come after AA.1. basically the descending sorting should be done like:
Sorted Version:
BB
AA
Z
C
B
A
I’ve already tried the
Alphanum Algorithm discussed at http://www.DaveKoelle.com as well
Natural Sort Comparer at
http://www.codeproject.com/Articles/22517/Natural-Sort-Comparer. It does everything what I want except the sorting mentioned above.
So, this looks like you need to sort on some custom logic as followed in the CSV file. To perform this you must implement
IComparer<string>on a class and implement theComparemethod based on your requirement. Looks like in your case, you need to first split the string into two parts, the alphabets part and numeric part (using regular expression), then compare the string first, if both are same then compare number part and return the value accordingly.You can then use this
Comparerclass to sort them.Update
Code sample
To use this Comparer
Caution: I have proved the above code is only correct and not otherwise. There can be some edge cases where it may not be working as expected