1

I have the following values in a List that is called xAlleles (The values are added to the list as they are read from a file, so they are not necessarily provided in the order that is ultimately required. There is no possibility of them being in the correct order in the file as the files comes from some third party software over which I have no control or influence)

DQB1*03:01
DQB1*03:294
DQB1*03:276N
DQB1*03:292
DQB1*03:297
DQB1*03:87

which I need to get into the following order

DQB1*03:01
DQB1*03:87
DQB1*03:276N
DQB1*03:292
DQB1*03:293
DQB1*03:294
DQB1*03:297

i.e. The values are in ascending numerical order.

I'm aware of the following linq method:-

xAlleles = xAlleles.OrderBy(x => x.Length).ThenBy(x => x).ToList();

This generally works but when the value has an alphabetic suffix (that cannot be removed as it is valid and correct) it places the DQB1*03:276N value at the end of the list rather than in it's position numerically.

I'm sure there's a way to do this but I've spent a long time getting nowhere with it and hope that the community can come up with some suggestions.

Sam Axe
  • 33,313
  • 9
  • 55
  • 89
Neil
  • 163
  • 1
  • 2
  • 17
  • 1
    It seems you want a [natural sort](https://stackoverflow.com/questions/9988937/sort-string-numbers). – MakePeaceGreatAgain Oct 28 '19 at 16:19
  • So your `N` is always at the last of string? – er-sho Oct 28 '19 at 16:20
  • This answer seems the best for your question: https://stackoverflow.com/a/1404415/856353 A fairly generic implementation of a natural IComparer. – Robert McKee Oct 28 '19 at 17:12
  • Thanks for your efforts guys. Once I saw that it was a duplicated question (I obviously didn't search on the right terms) I went to the suggested answer sort string-numbers [duplicate] and it worked fine. – Neil Oct 28 '19 at 17:19

0 Answers0