Optimize the <T> .Sort (Comparer) List

I have a list that stores lost integers. I don't like the way List.Sort () works by default, as I want the list to be sorted by the size of the actual int. So far I have this:

Oh, and ints are stored in strings, like "1234". This is something that I can’t change.

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;
            }
        }
    }
}

But as far as I know, this is a bubble sort, right? What should I implement instead? Quicksort? In addition, I may need help writing this document.

Oh, and my list contains no more than 2 thousand elements that store numbers in strings

In addition, I call my IComparer as follows:

IntComparer intSort = New IntComparer();
List<T>.Sort(intSort);
+3
source share
6

, , , - :

numbers.Sort((x,y) => Int32.Parse(x).CompareTo(Int32.Parse(y)));
+6

, . , , , . MSDN, List.Sort - quicksort.

+4

, , ints , , ints ?

, , , - Linq :

System;

using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication6
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var unsortedListOfStringsAsInts = new List<string> {"1234", "2345", "7", "9"};
            var sortedListOfInts = unsortedListOfStringsAsInts.Select(x => int.Parse(x)).OrderBy(x => x).ToList();

            foreach (var i in sortedListOfInts)
                Console.WriteLine(i);
        }
    }
}

, 2 - , , .

+1

, , , - QuickSort, .

List<T>.Sort

Array.Sort, QuickSort.

:

public class IntComparer : IComparer<string> {

    private static int ParseInt32(string text) {
        long value = 0;
        foreach (char c in text) {
                if (c >= '0' && c <= '9') {
                value = value * 10 + c - '0';
            } else {
                throw new FormatException();
            }
        }
        if (value > int.MaxValue) throw new OverflowException();
        return (int)value;
    }


    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.
                //
                if (x.Length != y.Length) {
                    // If the strings are not of equal length,
                    // the longer string is greater.
                    return x.Length - y.Length;
                } else {
                    // compare numerically
                    int xInt = ParseInt32(x);
                    int yInt = ParseInt32(y);
                    return xInt - yInt;
                }
            }
        }
    }

}

Edit:
. , , .

+1

I think you should first convert stringto a temporary list int, since the other code here (so far) will convert the strings over and over again, for each comparison. (You can also use nullable ints if it's important to maintain nullaround). After that, you sort the list and, if necessary, convert back to strings.

+1
source

Here is a quick example if you are using a .net 3.5 (Linq) project

using System.Linq;
using System.Collections.Generic;

List<string> unorderedList = List<string>();
list.Add("3");
list.Add("5");
list.Add("2");
list.Add("10");
list.Add("-6");
list.Add("7");

List<string> orderedList = list.OrderBy(x => int.Parse(x)).ToList();
0
source

Source: https://habr.com/ru/post/1706805/


All Articles