c# - Sorting IEnumerable<string> -
this question has answer here:
- natural sort order in c# 12 answers
i've got ienumerable<string>
values like:
- pp
- pp.10.0
- pp.6.0
- pp.6.1
- pp.8.3
- kp.10.1
- kp
- kp.3.20
... etc
i'd organize them alphabetically, keeping values double digits (in example above ones 10.0 , 10.1) below ones single digits. in other words, after sorting should this:
- kp
- kp.3.20
- kp.10.1
- pp
- pp.6.0
- pp.6.1
- pp.8.3
- pp.10.0
i tried simple ienumerable<string>.orderby(x => x, stringcomparer.currentcultureignorecase)
doesn't quite work in case.
also bear in mind pp
, kp
parts could have dots in them well, if you're thinking of splitting string.
just create own comparer , pass overload of orderby
: https://msdn.microsoft.com/de-de/library/bb549422(v=vs.110).aspx
for example:
ienumerable<string>.orderby(x => x, new mycomparer());
your own comparer can class (in example above named mycomparer
) implements interface system.collections.generic.icomparer<string>
. put compare logic method compare
.
class mycomparer : icomparer<string> { public int compare(string x, string y) { // logic here } } // ... ienumerable<string> mylistofstrings = ...; mylistofstrings.orderby(x => x, new mycomparer());
Comments
Post a Comment