# Sorting a two-dimensional array in C #

• ### Question

• Hi there!

There is a two-dimensional array:

`public dynamic[,] matrix = new dynamic[x, y];`
where x and y can have any values. I need to sort this array by several columns (just like sort data in a range or table in Excel). How can I do this?

Monday, November 26, 2018 12:34 PM

### Answers

• Thanks, Andrey. But bubble-sort is very slow. I found this way:

1. Translate two-dimensional array into jagged array:

```            dynamic[][] temp = new dynamic[x][];

for (int i = 0; i < x; i++)
{
temp[i] = new dynamic[y];

for (int j = 0; j < y; j++)
{
temp[i][j] = matrix[i, j];
}
}```

2. Use Array.Sort:

`Array.Sort(temp, (a, b) => compareForSort(a, b));`

where compareForSort - custom comparison function. In my case it is:

```        private int compareForSort(dynamic[]a, dynamic[]b)
{
if (a[10] != b[10])
{
return a[10].CompareTo(b[10]) > 0 ? 1 : -1;
}
else if (a[12] != b[12])
{
return a[12].CompareTo(b[12]) > 0 ? 1 : -1;
}
else if (a[15] != b[15])
{
return a[15] > b[15] ? 1 : -1;
}
else if (a[14] != b[14])
{
return a[14].CompareTo(b[14]) > 0 ? 1 : -1;
}
else if (a[85] != b[85])
{
return a[85] > b[85] ? 1 : -1;
}

return 0;
}```

3. Translate jagged array into two-dimensional array:
```            for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
matrix[i, j] = temp[i][j];
}
}```

• Marked as answer by Tuesday, November 27, 2018 9:23 AM
Tuesday, November 27, 2018 9:22 AM

### All replies

• Hello,

How can I do this?

With usual way.

Simplest - bubble-sort with conditions included several columns.

Sincerely, Highly skilled coding monkey.

Monday, November 26, 2018 2:09 PM
• Hi Demid_T,

Thank you for posting here.

For your question, if you want to sort a two-dimensional array, here is a simple example for your reference.

```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace test2
{
class Program
{
static void Main(string[] args)
{
dynamic[,] matrix = new dynamic[4,3]{ { 1, 3, 2 }, { 16, 9, 4 }, { 13, 8, 500 }, { 10, 11, 12 } };
int [,]a= { { 1, 3, 2 }, { 16, 5, 4 }, { 13, 8, 9 }, { 10, 11, 12 } };
Console.WriteLine("Unsorted array");
Print(matrix);
int j;
for (j = 0; j < 3; j++)
{
int m, n;
int t;
for (m = 3; m > 0; m--)
{
for (n = 0; n < m; n++)
{
if(matrix[n,j]>matrix[n+1,j])
{
t = matrix[n, j];
matrix[n, j] = matrix[n + 1, j];
matrix[n + 1, j] = t;
}
}
}
}
Console.WriteLine("have sorted array");
Print(matrix);
Console.ReadKey();
}
static void Print(object[,] values)
{
int k;
for (int i = 0; i < values.GetLength(0); i++)
{
for (k = 0; k < values.GetLength(1); k++)
{
Console.Write(values[i, k]);
Console.Write("  ");
}
}
}
}
}
```

Best Regards,

Wendy

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

Tuesday, November 27, 2018 6:37 AM
• Thanks, Andrey. But bubble-sort is very slow. I found this way:

1. Translate two-dimensional array into jagged array:

```            dynamic[][] temp = new dynamic[x][];

for (int i = 0; i < x; i++)
{
temp[i] = new dynamic[y];

for (int j = 0; j < y; j++)
{
temp[i][j] = matrix[i, j];
}
}```

2. Use Array.Sort:

`Array.Sort(temp, (a, b) => compareForSort(a, b));`

where compareForSort - custom comparison function. In my case it is:

```        private int compareForSort(dynamic[]a, dynamic[]b)
{
if (a[10] != b[10])
{
return a[10].CompareTo(b[10]) > 0 ? 1 : -1;
}
else if (a[12] != b[12])
{
return a[12].CompareTo(b[12]) > 0 ? 1 : -1;
}
else if (a[15] != b[15])
{
return a[15] > b[15] ? 1 : -1;
}
else if (a[14] != b[14])
{
return a[14].CompareTo(b[14]) > 0 ? 1 : -1;
}
else if (a[85] != b[85])
{
return a[85] > b[85] ? 1 : -1;
}

return 0;
}```

3. Translate jagged array into two-dimensional array:
```            for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
matrix[i, j] = temp[i][j];
}
}```

• Marked as answer by Tuesday, November 27, 2018 9:23 AM
Tuesday, November 27, 2018 9:22 AM