none
Issue with OrderBy for Strings with Integers RRS feed

  • Question

  • Hi there,

    I've noticed an issue while setting up some demo data I haven't noticed before. If I have a string which is the same but only a number at the end is different, OrderBy() seems to sort wrong.

    So the data would look like the following after OrderBy():

    • Test1
    • Test10
    • Test11
    • Test12
    • ....
    • Test2
    • Test20
    • Test21
    • ...

    I'm pretty sure I'm not the first one noticing it and because I have no clue about how to search for this on the web I wanted to ask here how to solve such "issue".

    Cheers
    Tom

    Thursday, June 16, 2016 3:24 PM

Answers

  • Hi Tom_O,

    >>I'm pretty sure I'm not the first one noticing it and because I have no clue about how to search for this on the web I wanted to ask here how to solve such "issue".

    If the value before integer is fixed, we could capture the number of data and cast then to integer, and sort it. like this:

    var query = db.Classes.ToList()
                        .OrderBy(c => int.Parse(c.ClassID.Substring(4,c.ClassID.Length -4)));

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Tom_O Friday, June 17, 2016 8:13 AM
    Friday, June 17, 2016 1:16 AM
    Moderator

All replies

  • Hi there,

    I've noticed an issue while setting up some demo data I haven't noticed before. If I have a string which is the same but only a number at the end is different, OrderBy() seems to sort wrong.

    So the data would look like the following after OrderBy():

    • Test1
    • Test10
    • Test11
    • Test12
    • ....
    • Test2
    • Test20
    • Test21
    • ...

    I'm pretty sure I'm not the first one noticing it and because I have no clue about how to search for this on the web I wanted to ask here how to solve such "issue".

    Cheers
    Tom

    Maybe you need to sort it after the data/objects are loaded into a disconnected collection.

     
    Thursday, June 16, 2016 9:50 PM
  • That's the correct alphanumeric sort for strings. 'A' < 'AB' < 'B', and so 'Test1' < 'Test10' < 'Test2'

    Either extract a number to sort on, or format the strings so they sort like you want.  eg 'Test01' instead of 'Test1'

    David


    David http://blogs.msdn.com/b/dbrowne/

    Thursday, June 16, 2016 9:55 PM
  • Hi Tom_O,

    >>I'm pretty sure I'm not the first one noticing it and because I have no clue about how to search for this on the web I wanted to ask here how to solve such "issue".

    If the value before integer is fixed, we could capture the number of data and cast then to integer, and sort it. like this:

    var query = db.Classes.ToList()
                        .OrderBy(c => int.Parse(c.ClassID.Substring(4,c.ClassID.Length -4)));

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Tom_O Friday, June 17, 2016 8:13 AM
    Friday, June 17, 2016 1:16 AM
    Moderator
  • That's the correct alphanumeric sort for strings. 'A' < 'AB' < 'B', and so 'Test1' < 'Test10' < 'Test2'

    Either extract a number to sort on, or format the strings so they sort like you want.  eg 'Test01' instead of 'Test1'

    David


    David http://blogs.msdn.com/b/dbrowne/


    I know, but I thought there might be a built-in option in Entity Framework for recognizing such situation without additional work. Maybe an idea for the future development of EF.
    Friday, June 17, 2016 8:12 AM
  • That's a good idea if characters are fixed. Though it would also be possible for different strings, just a bit different.

    Nevertheless, I wasn't aware of this possibility for OrderBy therefore it's a good starting point.

    Friday, June 17, 2016 8:13 AM