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.
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.
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