views:

520

answers:

3

Hi,

What's the easiest way to get a LINQ query (from an SQL database - does that matter?) to order strings naturally?

For example, I'm currently getting these results:

  • Project 1
  • Project 10
  • Project 2

What I'd like is to see is this:

  • Project 1
  • Project 2
  • Project 10

The query I'm using is this:

return from p in dataContext.Projects
    orderby p.Name
    select p;
+6  A: 

There is no built-in way to do this using the .NET framework but I would suggest that you read Natural Sorting in C# for a discussion on the topic and an open-source implementation.

Andrew Hare
+1.awesome link.
Stan R.
Nice find, thanks.
Kieron
A: 

If there is an ID column, which matches with the order in which items are added, you could use that for sorting.

e.g. if the ID and ProjectName is as below
1 | Project 1
2 | Project 2
4 | Project 4

shahkalpesh
+2  A: 

This is what you really need.

http://zootfroot.blogspot.com/2009/09/natural-sort-compare-with-linq-orderby.html?showComment=1258020708758#c5034722582649839449

John Grymonprez
Thanks for the link dude :)
James McCormack