优化列表<T> .Sort(Comparer)

Cas*_*erT 3 c# sorting generics collections

我有一个存储丢失整数的列表.我不喜欢默认的List.Sort()工作,因为我希望列表按实际int的大小排序.到目前为止我有这个:

哦,并且整数存储在字符串中,例如"1234".这是我无法改变的.

public class IntComparer : IComparer<string>
{
    public int Compare(string x, string y)
    {
        if (x == null)
        {
            if (y == null)
            {
                // If x is null and y is null, they're
                // equal. 
                return 0;
            }
            else
            {
                // If x is null and y is not null, y
                // is greater. 
                return -1;
            }
        }
        else
        {
            // If x is not null...
            //
            if (y == null)
            // ...and y is null, x is greater.
            {
                return 1;
            }
            else
            {
                // ...and y is not null, compare the 
                // lengths of the two strings.
                //
                int xInt = Convert.ToInt32(x);
                int yInt = Convert.ToInt32(y);

                if (x > y)
                {
                    // If the strings are not of equal length,
                    // the longer string is greater.
                    //
                    return 1;
                }
                else if (xInt == yInt)
                {
                    return 0;
                }
                else
                {
                    // If the strings are of equal length,
                    // sort them with ordinary string comparison.


        //
                return -1;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但据我所知,这是泡沫排序,对吗?我该怎么办呢?快速排序?另外,我可能需要帮助写它.

哦,我的列表包含2千个元素,用于存储字符串中的数字

另外,我这样调用我的IComparer:

IntComparer intSort = New IntComparer();
List<T>.Sort(intSort);
Run Code Online (Sandbox Code Playgroud)

Bri*_*sen 6

假设您想要按存储为字符串的整数值进行排序,您可以简单地执行以下操作:

numbers.Sort((x,y) => Int32.Parse(x).CompareTo(Int32.Parse(y)));
Run Code Online (Sandbox Code Playgroud)