-2

I'm trying to figure out how to sort a 2 dimensional array. I've seen this question posted for other languages, but not C#. I'm working on a coding challenge where the input is in the form of a 2 dimensional array and I need to sort by the first column.

I'm thinking to create an extension method for a 2 dimensional int array that takes a parameter for the left column or the right, but not sure what would be be the best sorting algorithm for that. I figured I could convert the 2D int array to a SortedDictionary then convert it back, but it seems like it would be inefficient if I convert it back to a 2D array again. Would it be best to convert to a SortedDictionary and leave it in that data type or does someone have an amazing sorting algorithm for 2D arrays?

public static int[][] Sort(this int[][] a2DArr, bool sortLeftCol = true)
{
    SortedDictionary<int, int> sd = new SortedDictionary<int, int>();
    int[][] sorted2Darr = new int[a2DArr.Count()][];

    if (sortLeftCol == true)
    {
        for (int i = 0; i < a2DArr.Count(); i++)
        { sd.Add(a2DArr[i][0], a2DArr[i][1]); }

        for (int i = 0; i < a2DArr.Count(); i++)
        {
            sorted2Darr[i][0] = sd.ElementAt(i).Key;
            sorted2Darr[i][1] = sd.ElementAt(i).Value;
        }
    }
    else //sort by right column
    {
        for (int i = 0; i < a2DArr.Count(); i++)
        { sd.Add(a2DArr[i][1], a2DArr[i][0]); }

        for (int i = 0; i < a2DArr.Count(); i++)
        {
            sorted2Darr[i][0] = sd.ElementAt(i).Value;
            sorted2Darr[i][1] = sd.ElementAt(i).Key;
        }
    }

    return sorted2Darr;
}
halfer
  • 19,824
  • 17
  • 99
  • 186
SendETHToThisAddress
  • 2,756
  • 7
  • 29
  • 54
  • 2
    maybe `a2DArr.OrderBy(x => x[0])` ? – TheGeneral Sep 14 '20 at 00:36
  • 1
    Also found that but here you have a jagged array (an array of arrays), not a multi-dimentional array 2D... and this is not really the same thing : https://www.tutorialspoint.com/how-do-i-sort-a-two-dimensional-array-in-chash & https://social.msdn.microsoft.com/Forums/vstudio/en-US/57691712-3064-4244-954a-1ada2d1e3020/how-to-sort-the-2d-array-according-to-specific-column?forum=csharpgeneral & https://www.c-sharpcorner.com/article/how-to-sort/ –  Sep 14 '20 at 00:40
  • @MichaelRandall I tried that but it sorted by the 2nd/right column, what I need to do is sort by the Left column (column 0). I think .OrderBy is meant for 1 dimensional arrays, but that code snippet seems useful for sorting by the right column. – SendETHToThisAddress Sep 14 '20 at 01:04
  • Not sure i follow, https://dotnetfiddle.net/7z89fI – TheGeneral Sep 14 '20 at 01:09

1 Answers1

0

I found an answer posted where someone had asked a similar question but about rectangular arrays. This is a nice little method using Array.Sort with a lambda expression:

Array.Sort(contests, (a, b) => { return a[0] - b[0]; });

Which will sort a 2D array by the first column and keep the corresponding data in the second column (like sort rows by ID). If any row has the same value on the left side but different values on the right, then it will sort by value in the second column within those rows.

If you enter a 1 for the first value then it will sort by the right column:

Array.Sort(contests, (a, b) => { return a[1] - b[0]; });
SendETHToThisAddress
  • 2,756
  • 7
  • 29
  • 54