Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am trying to sort the values which is actually a seminumeric string.When i try to sort the values using LINQ i cant able to get the proper sorted data.

Say.if i am sorting the value in ascending order{
VB
 "121212-1","121212-7","121212-6","121212-16",121212-4",
"121212-15",
"121212-14"

}
with the query .OrderBy(list => list)

I am getting the proper output.KIndly help me to sort the seminumeric number.

I ahve tried the solution provided here

[^]
but it didnot helped me...

Regards,
Hsakarp.
Posted

The problem is that you are trying to sort strings as if they were numbers - that doesn't work, because the sort order is different. With strings, the sort order goes:
1
10
11
12
...
19
2
20
...
What you need to do is convert them to numbers are sort those. Probably, the best way is to use a class, but a quick and dirty method would be to do this:
C#
List<string> list = new List<string>(){ "121212-1","121212-7","121212-6","121212-16","121212-4","121212-15","121212-14"};
list = list.OrderBy(x => int.Parse(x.Split('-')[0])).ThenBy(x => int.Parse(x.Split('-')[1])).ToList();
foreach (string s in list)
    {
    Console.WriteLine(s);
    }
But you would need to be sure that every string was in the right format first or you will get null reference or parse errors!
 
Share this answer
 
You'll have to split the strings and order them as integers, like so:

C#
IList<string> list = new List<string> 
    {
        "121212-1",
        "121212-7",
        "121212-6",
        "121212-16",
        "121212-4", 
        "121212-15", 
        "121213-14", 
        "121212-14"
    };
	
    Console.WriteLine("unsorted");
    foreach(string s in list) Console.WriteLine(s);

    /**
        Output:
        
        121212-1
        121212-7
        121212-6
        121212-16
        121212-4
        121212-15
        121213-14
        121212-14
    **/
    
    IList<string> sortedList = list
        .Select(s => s.Split(new[]{"-"}, StringSplitOptions.RemoveEmptyEntries))
        .OrderBy(a => Int32.Parse(a[0]))
        .ThenBy(a => Int32.Parse(a[1]))
        .Select(a => String.Join("-", a))
        .ToList();
    
    Console.WriteLine("sorted");
    foreach(string s in sortedList) Console.WriteLine(s);

    /**
        Ouput:
        
        121212-1
        121212-4
        121212-6
        121212-7
        121212-14
        121212-15
        121212-16
        121213-14
    **/
 
Share this answer
 
v2
Comments
CPallini 14-Aug-13 4:29am    
5.
Nandakishore G N 14-Aug-13 5:10am    
Good answer. +5..
hsakarp 14-Aug-13 7:05am    
Thanks for quick reply..i have tried to replicate the same with my model class.while converting that into the list i ma getting the error "cannot convert the string to model class".I have tried changing the class also cant able to figure out the way to achieve..

Currently i am sorting the entire list with a column called activity

ASList
//.OrderBy(list => DateTime.Parse(list.CreatedDate))
//.OrderBy(list => list.ActivityID)
.Select(list => list.ActID.Split(new [] { "-" }, StringSplitOptions.RemoveEmptyEntries))
.OrderBy(a => Int32.Parse(a[0]))
.ThenBy(a => Int32.Parse(a[1]))
.Select(a => String.Join("-", a))
.OrderBy(list => DateTime.Parse(list.CreatedDate))
".ToList()";

If i change the string to the model class i dont have the access to the split option.could you share me the way to achieve this

Regards,
Hsakarp
Maarten Kools 14-Aug-13 7:26am    
You're creating a list of Strings with the second Select. You'll have to write it differently if the string to sort is encapsulated in an object.

e.g.
var sortedList = (from i in list
let id = i.ActID.Split(new [] { "-" }, StringSplitOptions.RemoveEmptyEntries)
orderby Int32.Parse(id[0]), Int32.Parse(id[1])
select i)
.ToList()
hsakarp 14-Aug-13 8:04am    
superbb!!!..

Many thanks..:)
(If I got you) you have to parse each 'seminumeric' item in order to separate the two numeric parts (e.g. "121212-1" => fn=121212, ln=1) and then order the items based on such numeric parts (first considering fn and then ln).
It should be pretty straightforward.
 
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