使用IComparable对对象进行排序

时间:2013-02-01 00:45:03

标签: c# list icomparable

我正在尝试在自定义对象中实现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的工作方式,但基本上我的列表的排序方式如下:

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

我希望他们像这样排序:

  • 测试升级
  • 测试升级2
  • 测试升级3
  • ...等

5 个答案:

答案 0 :(得分:7)

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

杰夫在这里有一篇关于一些细节的好文章,链接到试图解决问题的不同算法:

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

基本上问题是:自然顺序整理需要解决人工智能问题;你试图模仿人类会做什么,这可能会让人感到非常棘手。祝你好运!

答案 1 :(得分:5)

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

答案 2 :(得分:3)

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

要获得您想要的效果,需要更多的工作。请参阅此问题:Sort on a string that may contain a number

答案 3 :(得分:2)

AlphaNumeric Sorting

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

答案 4 :(得分: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

不适用于Hello11Hello2等数据,因为它无法拆分它们。不区分大小写。