Click here to Skip to main content
15,893,564 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm new to LINQ concept the requirement what i have is, I have a string column with data as below

OR0
OR1
OR2
.
.
OR9
.
.
OR21
ORD

Now i want to order it as above, but when i do order by i get data as below as it is ordering as string

OR0
OR1
OR10
OR11
OR12
.
.
OR19
OR20
OR21
OR3
.
.
OR9
ORD

Which is wrong as per my requirement.

I tried ordering as Order By Len(Col),Col and the o/p what i received is

OR0
.
.
OR9
ORD
OR10
.
.
OR21

Can any one help me with giving me a proper Order By statement for LINQ statement?

Regards,
Kishan
Posted

As you've already no doubt found out, your sorted list would come out like this:

OR1
OR10
OR11
OR12
OR2
OR3

I would add another property to the object being sorted, which represents the integer following the alpha characters, and sort on that property instead.
 
Share this answer
 
v2
I don't know the VB syntax to do this, but with LINQ it would be fairly trivial to write a custom comparer that will take the numeric portion, sort on that, and return a collection that will be sorted as you want it to.
 
Share this answer
 
My previous answer got deleted because of some misunderstanding.

posting my answer again.


As per john said it's all correct but if you want to go to find it anyway then I have a trick for you.

Suppose your data is in List<string></string> for applying linq operation.

So you can get sorted data for your case with Linq like,

C#
List<string> list = new List<string>();
..
..
var SortedList  = from l in list
                  orderby l.ToString()+"0"
                  select l;
 
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