Tuesday, July 6, 2010

C# Sort Text and Numbers (like Explorer does)

For example:
myvideo.part2.rar
Should come before:
myvideo.part10.rar


// Usage:

list.Sort(new StringLogicalComparer());


// Comparer:

public class StringLogicalComparer : IComparer, IComparer<string>
{
    [DllImport("shlwapi.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
    public static extern int StrCmpLogicalW(string x, string y);

    public int Compare(object x, object y)
    {
        return StrCmpLogicalW(x.ToString(), y.ToString());
    }

    public int Compare(string x, string y)
    {
        return StrCmpLogicalW(x, y);
    }
}

1 comment:

  1. I discovered your weblog web site on google and test just a few of your early posts. Continue to maintain up the superb operate. I simply additional up your RSS feed to my MSN Information Reader. Seeking forward to studying extra from you later on!… betfair online casino

    ReplyDelete