0

The problem is that the below list contains alphanumeric data, and also it has data like 6.1 and 6.10 which is same in terms of integer but not same as a string.

Problem List

 6.1
 6.10
 6.11
 6.12
 6.2
 6.3
 6.4 - a
 6.4 - b
 6.5
 6.6
 6.7
 6.8
 6.9 

Expected List

6.1
6.2
6.3
6.4 - a
6.4 - b
6.5
6.6
6.7
6.8
6.9
6.10
6.11
6.12 

Code

 var sortedData = ViolationCodesList.OrderBy(x => x.SubCategory).ToList();

Note : SubCategory is string ..

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
Danish
  • 497
  • 5
  • 14
  • 2
    You have to convert every string into a decimal in order to sort by it. Or is this actually a list of versions? In this case you may use the `Version`-class instead of decimal and order by that. – MakePeaceGreatAgain Nov 28 '17 at 10:55
  • 1
    One of the problems if you store multiple informations in a single (string) property. You can use the `Version` class as already mentioned and for the `a` and `b` another property – Tim Schmelter Nov 28 '17 at 10:58
  • @Richard , please look at the query. The link you marked duplicate has a different issue than mine. – Danish Nov 28 '17 at 11:00
  • No, the underlying issue is mixing numeric and non-numeric data in a single string. And the solution is the same: separate out the different parts when comparing (or pre-process to extract the search keys). – Richard Nov 28 '17 at 12:12
  • @Danish Try the solution to that question before you say it's not the same: `var sortedData = ViolationCodesList.OrderBy(x => PadNumbers(x.SubCategory)).ToList();` – Rufus L Nov 28 '17 at 12:14

0 Answers0