Question: I want to this list

```
A
B
C
111
11
123
1
42
5

```

To be sorted 

```
1
5
11
42
111
123
A
B
C

```

By default, it sorts numbers like strings (So, it goes 1,11,111,123,42,5),
But I want to sort numbers like numbers, and than strings that are not numbers.

Is there clean solution to sort it like above?

It is a list of objects, and object has several properties, one of which is a this string.

Answer: 
I wrote this `IComparer` implementation a few months back to handle something like this. I think it will do what you want by default, though it is built to handle more complex cases where number/letter groups are separated by delimiters that also need to be sorted atomically. You should be able to adjust it to your needs.

```
public class SemanticComparer : IComparer<string>
{
    private static Regex _splitter = new Regex("\\W+");

    public int Compare(string x, string y)
    {
        string[] partsX = _splitter.Split(x);
        string[] partsY = _splitter.Split(y);

        int shortest = Math.Min(partsX.Length, partsY.Length);

        for (int index = 0; index < shortest; index++)
        {
            int intX, intY;
            int result;

            if (int.TryParse(partsX[index], out intX) && int.TryParse(partsY[index], out intY))
            {
                result = intX.CompareTo(intY);
            }
            else
            {
                result = string.Compare(partsX[index], partsY[index], StringComparison.Ordinal);
            }

            if (result != 0)
            {
                return result;
            }
        }

        return 0;
    }
}

```

You can sort your list with it like this:

```
MyList.Sort(new SemanticComparer());

```
I haven't tested this code for performance, but you can solve this with a Comparer

```
public class ArrayItemComparer : IComparer<string>
{
    public int Compare(string x, string y)
    {
        int xInt = 0, yInt = 0;

        bool parseX = int.TryParse(x, out xInt);
        bool parseY = int.TryParse(y, out yInt);

        if (parseX && parseY)
        {
            return xInt.CompareTo(yInt);
        }
        else if (parseX)
        {
            return -1;
        }
        else if (parseY)
        {
            return 1;
        }
        else
        {
            return x.CompareTo(y);
        }
    }
}

```