0
Input -> {"rajesh|51|32|asd", "nitin|71|27|asd", "test|11|30|asd"} 

output -> {"test|11|30|asd", "rajesh|51|32|asd" ,"nitin|71|27|asd"}

How I can sort the above List<string> by using LINQ.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
SVK
  • 1
  • Possible duplicate: [Natural Sort Order in C#](https://stackoverflow.com/questions/248603/natural-sort-order-in-c-sharp) – Oliver Apr 11 '23 at 05:48

1 Answers1

0

you can get number from string using Regex.Match(inputString, @"\d+") and sort the list by this number using linq.OrderBy:

List<string> list = new List<string>() { "rajesh|51|32|asd", "nitin|71|27|asd", "test|11|30|asd" };
var result=list.OrderBy(i => Regex.Match(i, @"\d+").Value).ToList();

result:

1=> "test|11|30|asd"
2=>"rajesh|51|32|asd" 
3=>"nitin|71|27|asd"

Note: There are two numbers in the string and you didn't say which number to sort by. But the result shows that you mean the first number

Hossein Sabziani
  • 1
  • 2
  • 15
  • 20