Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I sort a two-dimensional (rectangular) array in C#?

Tags:

arrays

c#

sorting

I have a two-dimensional array (of Strings) which make up my data table (of rows and columns). I want to sort this array by any column. I tried to find an algorithm for doing this in C#, but have not been successful.

Any help is appreciated.

like image 295
Jack Avatar asked Oct 24 '08 03:10

Jack


People also ask

Can you sort a 2D array in C?

You can simply use STL to sort 2D array row-wise..

How do you sort a 2D array?

Make the 2D array into a separate simple (1D) array (STEP 1). Then use the Arrays. sort() method to sort the simple array (STEP 2). Then set each space of the 2D array to be the number of columns across (X-coordinate where the space will be changed) multiplied by the number of spaces per row in the 2D array.

What happens when you sort a 2D array?

Rearranges the rows or columns of a 2D array by sorting the elements in the specified column or row in ascending order. This VI rearranges the elements in the specified column or row in ascending order by comparing the elements using comparison rules for different data types.

Are two-dimensional arrays rectangular?

You can think of a rectangular array as a table, where the first dimension is the number of rows and the second dimension is the number of columns keeping in mind that every row is the same length. Due to 2 dimensions, it's also known as two-dimensional array or 2D array.


1 Answers

Can I check - do you mean a rectangular array ([,])or a jagged array ([][])?

It is quite easy to sort a jagged array; I have a discussion on that here. Obviously in this case the Comparison<T> would involve a column instead of sorting by ordinal - but very similar.

Sorting a rectangular array is trickier... I'd probably be tempted to copy the data out into either a rectangular array or a List<T[]>, and sort there, then copy back.

Here's an example using a jagged array:

static void Main() {  // could just as easily be string...     int[][] data = new int[][] {          new int[] {1,2,3},          new int[] {2,3,4},          new int[] {2,4,1}      };      Sort<int>(data, 2);  }  private static void Sort<T>(T[][] data, int col)  {      Comparer<T> comparer = Comparer<T>.Default;     Array.Sort<T[]>(data, (x,y) => comparer.Compare(x[col],y[col]));  }  

For working with a rectangular array... well, here is some code to swap between the two on the fly...

static T[][] ToJagged<T>(this T[,] array) {     int height = array.GetLength(0), width = array.GetLength(1);     T[][] jagged = new T[height][];      for (int i = 0; i < height; i++)     {         T[] row = new T[width];         for (int j = 0; j < width; j++)         {             row[j] = array[i, j];         }         jagged[i] = row;     }     return jagged; } static T[,] ToRectangular<T>(this T[][] array) {     int height = array.Length, width = array[0].Length;     T[,] rect = new T[height, width];     for (int i = 0; i < height; i++)     {         T[] row = array[i];         for (int j = 0; j < width; j++)         {             rect[i, j] = row[j];         }     }     return rect; } // fill an existing rectangular array from a jagged array static void WriteRows<T>(this T[,] array, params T[][] rows) {     for (int i = 0; i < rows.Length; i++)     {         T[] row = rows[i];         for (int j = 0; j < row.Length; j++)         {             array[i, j] = row[j];         }     } } 
like image 105
Marc Gravell Avatar answered Oct 27 '22 00:10

Marc Gravell