Click here to Skip to main content
15,897,226 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I am new one to LINQ, I have one task to sort the content the content may stored like this

VB
list1(1909-1930,1921-1925,1907–21,1871–1924,2009-10,456–458,58–66)


how to sort this like

58–66
456–458
1871–1924
1907–21
1909-1930
1921-1925


any one give explanation how to solve it
Posted
Comments
Andrew Perkins 6-Aug-15 11:35am    
Is the list is a list of strings?

1 solution

Exactly how you do this will depend on how they are stored.
If those are strings, then it's a little complicated, but it is still possible. Assuming that you want numerical order, first by 1900, then by 1930:
C#
List<string> list1 = new List<string>() {"1909-1930","1921-1925","1907-21","1871-1924","2009-10","456-458","58-66"};
list1 = list1.OrderBy(s => int.Parse(s.Substring(0, s.IndexOf('-'))))
             .ThenBy(s => int.Parse(s.Substring(s.IndexOf('-'))))
             .ToList();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900