1

我正在尝试IComparable在我的自定义对象中实现接口,以便List.Sort()可以按字母顺序对它们进行排序。

我的对象有一个称为_name字符串类型的字段,我希望它以此为基础进行排序。这是我实现的方法:

    public int CompareTo(object obj)
    {
        //Int reference table:
        //1 or greater means the current instance occurs after obj
        //0 means both elements occur in the same position
        //-1 or less means the current instance occurs before obj

        if (obj == null)
            return 1;

        Upgrade otherUpgrade = obj as Upgrade;

        if (otherUpgrade != null)
            return _name.CompareTo(otherUpgrade.Name);
        else
            throw new ArgumentException("Passed object is not an Upgrade.");
    }

不确定我是否做错了什么,或者这只是字符串的CompareTo工作方式,但基本上我的 List 是这样排序的:

  • 测试升级
  • 测试升级 10
  • 测试升级 11
  • 测试升级 12
  • 测试升级 13
  • 测试升级 14
  • 测试升级 15
  • 测试升级 2
  • 测试升级 3
  • 测试升级 4
  • 测试升级 5

我希望它们像这样排序:

  • 测试升级
  • 测试升级 2
  • 测试升级 3
  • ...ETC
4

5 回答 5

7

你想要“自然顺序”——熟悉英语惯例的人会选择的排序规则——而不是你所拥有的“词典”排序规则:为每个字母分配一个严格的顺序,然后依次按每个字母排序。

Jeff 有一篇关于这里的来龙去脉的好文章,其中包含尝试解决问题的不同算法的链接:

http://www.codinghorror.com/blog/2007/12/sorting-for-humans-natural-sort-order.html

Raymond 在这里讨论了 Windows 是如何处理它的:

http://technet.microsoft.com/en-us/magazine/hh475812.aspx

基本上问题是:自然顺序排序需要解决一个人工智能问题;您正在尝试模仿人类会做什么,这可能会非常棘手。祝你好运!

于 2013-02-01T00:57:54.653 回答
5

字符串按字典顺序排序。您必须将所有数字格式化为具有相同长度(例如:Test Upgrade 02)或在比较器中解析数字并将其合并到比较逻辑中。

于 2013-02-01T00:51:11.013 回答
3

发生这种情况的原因是您正在进行字符串比较,它没有明确的数字知识。它按每个字符的相应字符代码对每个字符串进行排序。

要获得您想要的效果需要更多的工作。请参阅此问题:对可能包含数字的字符串进行排序

于 2013-02-01T00:51:15.027 回答
2

字母数字排序

public class AlphanumComparatorFast : IComparer
{
    public int Compare(object x, object y)
    {
    string s1 = x as string;
    if (s1 == null)
    {
        return 0;
    }
    string s2 = y as string;
    if (s2 == null)
    {
        return 0;
    }

    int len1 = s1.Length;
    int len2 = s2.Length;
    int marker1 = 0;
    int marker2 = 0;

    // Walk through two the strings with two markers.
    while (marker1 < len1 && marker2 < len2)
    {
        char ch1 = s1[marker1];
        char ch2 = s2[marker2];

        // Some buffers we can build up characters in for each chunk.
        char[] space1 = new char[len1];
        int loc1 = 0;
        char[] space2 = new char[len2];
        int loc2 = 0;

        // Walk through all following characters that are digits or
        // characters in BOTH strings starting at the appropriate marker.
        // Collect char arrays.
        do
        {
        space1[loc1++] = ch1;
        marker1++;

        if (marker1 < len1)
        {
            ch1 = s1[marker1];
        }
        else
        {
            break;
        }
        } while (char.IsDigit(ch1) == char.IsDigit(space1[0]));

        do
        {
        space2[loc2++] = ch2;
        marker2++;

        if (marker2 < len2)
        {
            ch2 = s2[marker2];
        }
        else
        {
            break;
        }
        } while (char.IsDigit(ch2) == char.IsDigit(space2[0]));

        // If we have collected numbers, compare them numerically.
        // Otherwise, if we have strings, compare them alphabetically.
        string str1 = new string(space1);
        string str2 = new string(space2);

        int result;

        if (char.IsDigit(space1[0]) && char.IsDigit(space2[0]))
        {
        int thisNumericChunk = int.Parse(str1);
        int thatNumericChunk = int.Parse(str2);
        result = thisNumericChunk.CompareTo(thatNumericChunk);
        }
        else
        {
        result = str1.CompareTo(str2);
        }

        if (result != 0)
        {
        return result;
        }
    }
    return len1 - len2;
    }
}

用法 :

using System;
using System.Collections;

class Program
{
    static void Main()
    {
    string[] highways = new string[]
    {
        "100F",
        "50F",
        "SR100",
        "SR9"
    };
    //
    // We want to sort a string array called highways in an
    // alphanumeric way. Call the static Array.Sort method.
    //
    Array.Sort(highways, new AlphanumComparatorFast());
    //
    // Display the results
    //
    foreach (string h in highways)
    {
        Console.WriteLine(h);
    }
    }
}

输出

50F
100F
SR9
SR100

于 2013-02-01T00:58:46.143 回答
0

谢谢大家的回复。我做了我自己的方法,它似乎工作正常。它不适用于所有情况,但适用于我的场景。这是任何有兴趣的人的代码:

    /// <summary>
    /// Compares the upgrade to another upgrade
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    public int CompareTo(object obj)
    {
        //Int reference table:
        //1 or greater means the current instance occurs after obj
        //0 means both elements occur in the same position
        //-1 or less means the current instance occurs before obj

        if (obj == null)
            return 1;

        Upgrade otherUpgrade = obj as Upgrade;

        if (otherUpgrade != null)
        {
            //Split strings into arrays
            string[] splitStringOne = _name.Split(new char[] { ' ' });
            string[] splitStringTwo = otherUpgrade.Name.Split(new char[] { ' ' });

            //Will hold checks to see which comparer will be used
            bool sameWords = false, sameLength = false, bothInt = false;

            //Will hold the last part of the string if it is an int
            int intOne = 0, intTwo = 0;

            //Check if they have the same length
            sameLength = (splitStringOne.Length == splitStringTwo.Length);

            if (sameLength)
            {
                //Check to see if they both end in an int
                bothInt = (int.TryParse(splitStringOne[splitStringOne.Length - 1], out intOne) && int.TryParse(splitStringTwo[splitStringTwo.Length - 1], out intTwo));

                if (bothInt)
                {
                    //Check to see if the previous parts of the string are equal
                    for (int i = 0; i < splitStringOne.Length - 2; i++)
                    {
                        sameWords = (splitStringOne[i].ToLower().Equals(splitStringTwo[i].ToLower()));

                        if (!sameWords)
                            break;
                    }
                }
            }

            //If all criteria is met, use the customk comparer
            if (sameWords && sameLength && bothInt)
            {
                if (intOne < intTwo)
                    return -1;
                else if (intOne > intTwo)
                    return 1;
                else //Both equal
                    return 0;
            }
            //Else use the default string comparer
            else
                return _name.CompareTo(otherUpgrade.Name);            
        }
        else
            throw new ArgumentException("Passed object is not an Upgrade.");
    }

将适用于使用“”字符隔开的字符串,如下所示:

测试数据:

  • 你好 11
  • 你好 2
  • 你好 13

结果

  • 你好 2
  • 你好 11
  • 你好 13

不适用于诸如Hello11和之类的数据,Hello2因为它无法拆分它们。不区分大小写。

于 2013-02-01T13:13:24.953 回答