Sort Array List items in C#

The Array List initially contains the following values:

Sorting Array List items.

    [0]:    N8
    [1]:    N4
    [2]:    N10
    [3]:    N11
    [4]:    N13
    [5]:    N0
    [6]:    N3
    [7]:    N2
    [8]:    N9
    [9]:    N6
    [10]:   N12
    [11]:   N1
    [12]:   N5
    [13]:   N7

Now i need to sort the array list such that it becomes

    [0]:    N0
    [1]:    N1
    [2]:    N2
    [3]:    N3
    [4]:    N4
    [5]:    N5
    [6]:    N6
    [7]:    N4
    [8]:    N8
    [9]:    N9
    [10]:   N10
    [11]:   N11
    [12]:   N12
    [13]:   N13

1 answer

  • answered 2018-04-17 06:12 Slaven Tojić

    Use LINQ OrderBy:

    string[] array = {"N8","N4","N10","N11","N13","N0","N3","N2","N9","N6","N12","N1","N5", "N7" };
    var result = array.OrderBy(x => Convert.ToInt32(x.Substring(1))).ToArray();
    

    DEMO HERE