Compare strings to consider numbers

I am trying to sort the nodes of the tree structure with respect to their text property, of course. The problem is that my comparison class does not care about numbers. Here is the code:

public class TreeNodeSorter : IComparer
{
    public int Compare(object x, object y)
    {
        var tx = x as TreeNode;
        var ty = y as TreeNode;

        return string.Compare(tx.Text, ty.Text);
    }
}

And here is the result:

enter image description here

The first child node (Debug ...) is fine, but my problem is why on earth "HBM \ D10" is sorted to "HBM \ D7", etc ....

+5
source share
4 answers

Read http://www.dotnetperls.com/alphanumeric-sorting . You may need to strip everything else to make them work, as they are sorted either numerically or alphabetically.

, , , .

0

, p/invoke StrCmpLogicalW(). Windows , :

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

    public int Compare(object x, object y)
    {
        var tx = x as TreeNode;
        var ty = y as TreeNode;

        return StrCmpLogicalW(tx.Text, ty.Text);
    }
}
+5

, char '1' , char '7. - , char .

, 0, :

D04
D07
D10
D11

....

0

( , - @ "\ D\d +" ), , , , , .

0

All Articles