'Sorting jagged array

var data = read();
switch (selector)
{
    case 1://accending
        data = data.OrderBy(inner => inner[2]).ToArray();
        drawChart();
        for (int i = 0; i < data[0].Length; i++)
        {
            for (int j = 0; j < data.Length; j++)
            {
            if (j != 0)
                {
                    Console.Write("     ");
                }
                Console.Write(data[j][i] + "  ");
            }
            Console.WriteLine();
        }
        complete = true;
        break;
    case 2://decending
        complete = true;
        break;
    default:
        Console.WriteLine("Not an option please enter a number between 1 and 2");
        break;
}

I have a staggered array called data which is being printed here, I need to be able to sort the arrays depending on a users input (case1 and 2)

say the data looks like this

jaggedarray[0] = new int[5] { 99, 999, 49, 79, 59 };
jaggedarray[1] = new int[3] { 199, 1999, 149 };
jaggedarray[2] = new int[2] { 999, 500 };

How can I sort it so the it sorts the 2nd column from highest to lowest then prints out the whole array?



Solution 1:[1]

Another method to the answer of Hari Prasad, which is a very good one.

You could sort using Array.Sort and use a Comparison targeted to the second column.

something like this:

var jaggedarray = new int[3][];

jaggedarray[0] = new int[5] { 99, 999, 49, 79, 59 };
jaggedarray[1] = new int[3] { 199, 1999, 149 };
jaggedarray[2] = new int[2] { 999, 500 };

Array.Sort(jaggedarray,  new Comparison<int[]>( 
    (x,y) => { return x[1] < y[1] ? -1 : (x[1] > y[1] ? 1 : 0); }
));

Solution 2:[2]

If I'm understanding you correctly and you have this input

var jagged = new int[3][]
{
    new int[3] { 6, 4, 5 },
    new int[3] { 2, 4, 1 },
    new int[3] { 3, 1, 2 }
};

And you're looking for this output (sorted on the third number i.e 2nd column index)

var jagged = new int[3][]
{
    new int[3] { 2, 4, 1 },
    new int[3] { 3, 1, 2 },
    new int[3] { 6, 4, 5 }
};

Then you can use this to do it:

var columnIdx = 2;
Array.Sort(jaggedArray, (x, y) => x[columnIdx].CompareTo(y[columnIdx]));

Solution 3:[3]

Compare based on the second element of the inner array

// Example array
int[][] intervals = new int[][] { new int[] { 1, 2 }, new int[] { 3, 5 }, new int[] { 4, 7 }, new int[] { 6, 8 }, new int[] { 9, 10 } };

// Sort by second element
Array.Sort(jaggedArray, (x, y) => x[1].CompareTo(y[1]));

Print Out the whole jagged Array

Console.WriteLine("[" + String.Join(",",intervals.ToList().Select(p=>"["+p[0].ToString()+","+p[1].ToString()+"]")) + "]");

Output: [[1,2],[3,8],[9,10]]

Solution 4:[4]

Use Skip to skip the first column inside OrderBy and take First to pick second column to sort on that element.

jaggedarray[0] = new int[5] { 99, 999, 49, 79, 59 };
jaggedarray[1] = new int[3] { 199, 1999, 149 };
jaggedarray[2] = new int[2] { 999, 500 };

jaggedarray = jaggedarray.OrderBy(j=>j.Skip(1).First()).ToArray(); // sort on second column.

Working Demo

Solution 5:[5]

Try this:

    int[][] jaggedarray = new int[3][];
    jaggedarray[0] = new int[5] { 99, 999, 49, 79, 59 };
    jaggedarray[1] = new int[3] { 199, 1999, 149 };
    jaggedarray[2] = new int[2] { 999, 500 };

    int arrayToSort = 2;
    int selector = 1;

    jaggedarray = Enumerable.Range(0, jaggedarray.Length).Select(x =>
    {
        if (x == arrayToSort)
        {
            if (selector == 2)
                    return jaggedarray[x].OrderByDescending(y => y).ToArray();
            return jaggedarray[x].OrderBy(y => y).ToArray();
        }
        return jaggedarray[x];
    }).ToArray();

    Console.WriteLine(jaggedarray);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dbuggy
Solution 2 MedievalCoder
Solution 3
Solution 4 Hari Prasad
Solution 5