Orderby() not ordering numbers correctly c#

后端 未结 12 1744
渐次进展
渐次进展 2020-12-15 03:08

I am writing an app for my company and am currently working on the search functionality. When a user searches for an item, I want to display the highest version (which is st

12条回答
  •  北荒
    北荒 (楼主)
    2020-12-15 03:29

    There's an awesome piece of code that does a great job when it comes to natural sorting. Its name is AlphanumComparator.

    Sample code:

    var ordered = Database.Cars.ToList().OrderBy(c => c.ModelString, new AlphanumComparator());
    

    Note that the list must be in memory.

    If you get the C# version, do this:

    AlphanumComparator : IComparer
    

    and

    public int Compare(string x, string y)
    

提交回复
热议问题