3
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?

5 Answers 5

4

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); }
));
Sign up to request clarification or add additional context in comments.

Comments

3

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]));

Comments

2

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]]

1 Comment

Your answer could be improved with additional supporting information. Please edit your answer to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

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

Comments

0

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

2 Comments

This works but only sorts the 1st column of the array? Is it possible to sort a column based on the user inputs. If the user inputs 2 for example it will sort by the 2nd column not the first, cheers
I see what you're saying. I've updated the code accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.