8

I have been hunting around for a solution to this for a while now.

When I sort the below using a string sort I have a list of:

10
10b
1111
1164
1174
23
23A
23B
23D
23E

I really want the list to be:

10
10b
23
23A
23B
23D
23E
1111
1164
1174

A numerical sort does not do the job either.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
user1106846
  • 81
  • 1
  • 3

3 Answers3

4

If you have LINQ, you can use OrderBy:

Regex digitPart = new Regex(@"^\d+", RegexOptions.Compiled);
...
myList.OrderBy(x => int.Parse(digitPart.Match(x).Value))
Ry-
  • 218,210
  • 55
  • 464
  • 476
  • 3
    Worked for me, but I had to change Integer.Parse to Int32.Parse, and .value should be capitalized. Syntax, meh... – Jagd Dec 19 '11 at 23:37
  • @Jagd: Oops :P I'm a VB.NET person and it's `Integer` there. – Ry- Dec 19 '11 at 23:42
  • It's not clear from the question whether the OP wants a full natural search that would also order "A1", "A2", "Bumblebee" in that order. – Jon Hanna Dec 20 '11 at 00:03
  • @minitech - Oh, duh. I should have realized that! *smacks head* :) – Jagd Dec 20 '11 at 16:19
3

The easiest is to wrap the Win32 API call as explained in https://stackoverflow.com/a/248613/631687

Community
  • 1
  • 1
John Arlen
  • 6,539
  • 2
  • 33
  • 42
3
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

public class NumStrCmp : IComparer<string> {
    public int Compare(string x, string y){
        Regex regex = new Regex(@"(?<NumPart>\d+)(?<StrPart>\D*)",RegexOptions.Compiled);
        var mx = regex.Match(x);
        var my = regex.Match(y);
        var ret = int.Parse(mx.Groups["NumPart"].Value).CompareTo(int.Parse(my.Groups["NumPart"].Value));
        if(ret != 0) return ret;
        return mx.Groups["StrPart"].Value.CompareTo(my.Groups["StrPart"].Value);
    }
}

class Sample {
    static public void Main(){
        var data = new List<string>() {"10","10b","1111","1164","1174","23","23A","23B","23D","23E"};
        data.Sort(new NumStrCmp());
        foreach(var x in data){
            Console.WriteLine(x);
        }
   }
} 
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70