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)
{
return 0;
}
else
{
return -1;
}
}
else
{
if (y == null)
{
return 1;
}
else
{
int xInt = Convert.ToInt32(x);
int yInt = Convert.ToInt32(y);
if (x > y)
{
return 1;
}
else if (xInt == yInt)
{
return 0;
}
else
{
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);
source
share