Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How do I sort a two-dimensional array in C#
Sorting a two-dimensional array in C# can be accomplished using several approaches. The most common methods include sorting individual rows using nested loops with bubble sort, using Array.Sort() for jagged arrays, or converting to a one-dimensional array for sorting.
Syntax
For sorting rows in a 2D array using nested loops −
for (int i = 0; i < arr.GetLength(0); i++) {
for (int j = 0; j < arr.GetLength(1) - 1; j++) {
for (int k = 0; k < arr.GetLength(1) - j - 1; k++) {
if (arr[i, k] > arr[i, k + 1]) {
// swap elements
}
}
}
}
For sorting jagged arrays using Array.Sort() −
Array.Sort(jaggedArray[rowIndex]);
Using Nested Loops with Bubble Sort
This approach sorts each row of the 2D array individually using the bubble sort algorithm −
using System;
class Program {
public static void Main() {
int[,] arr = {
{64, 34, 25, 12},
{22, 11, 90, 5},
{77, 30, 15, 8}
};
Console.WriteLine("Original Array:");
PrintArray(arr);
// Sort each row
for (int i = 0; i < arr.GetLength(0); i++) {
for (int j = 0; j < arr.GetLength(1) - 1; j++) {
for (int k = 0; k < arr.GetLength(1) - j - 1; k++) {
if (arr[i, k] > arr[i, k + 1]) {
int temp = arr[i, k];
arr[i, k] = arr[i, k + 1];
arr[i, k + 1] = temp;
}
}
}
}
Console.WriteLine("\nSorted Array (each row sorted):");
PrintArray(arr);
}
static void PrintArray(int[,] array) {
for (int i = 0; i < array.GetLength(0); i++) {
for (int j = 0; j < array.GetLength(1); j++) {
Console.Write(array[i, j] + " ");
}
Console.WriteLine();
}
}
}
The output of the above code is −
Original Array: 64 34 25 12 22 11 90 5 77 30 15 8 Sorted Array (each row sorted): 12 25 34 64 5 11 22 90 8 15 30 77
Using Array.Sort() with Jagged Arrays
For more efficient sorting, convert the 2D array to a jagged array and use Array.Sort() −
using System;
class Program {
public static void Main() {
int[][] jaggedArray = {
new int[] {64, 34, 25, 12},
new int[] {22, 11, 90, 5},
new int[] {77, 30, 15, 8}
};
Console.WriteLine("Original Jagged Array:");
PrintJaggedArray(jaggedArray);
// Sort each row using Array.Sort()
for (int i = 0; i < jaggedArray.Length; i++) {
Array.Sort(jaggedArray[i]);
}
Console.WriteLine("\nSorted Jagged Array:");
PrintJaggedArray(jaggedArray);
}
static void PrintJaggedArray(int[][] array) {
for (int i = 0; i < array.Length; i++) {
for (int j = 0; j < array[i].Length; j++) {
Console.Write(array[i][j] + " ");
}
Console.WriteLine();
}
}
}
The output of the above code is −
Original Jagged Array: 64 34 25 12 22 11 90 5 77 30 15 8 Sorted Jagged Array: 12 25 34 64 5 11 22 90 8 15 30 77
Sorting Entire 2D Array as Single Collection
To sort all elements in the 2D array as one collection, flatten it first, sort, then reconstruct −
using System;
using System.Linq;
class Program {
public static void Main() {
int[,] arr = {
{64, 34, 25},
{22, 11, 90},
{77, 30, 15}
};
Console.WriteLine("Original Array:");
PrintArray(arr);
// Flatten, sort, and reconstruct
int[] flatArray = arr.Cast<int>().OrderBy(x => x).ToArray();
int rows = arr.GetLength(0);
int cols = arr.GetLength(1);
int[,] sortedArray = new int[rows, cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
sortedArray[i, j] = flatArray[i * cols + j];
}
}
Console.WriteLine("\nEntire Array Sorted:");
PrintArray(sortedArray);
}
static void PrintArray(int[,] array) {
for (int i = 0; i < array.GetLength(0); i++) {
for (int j = 0; j < array.GetLength(1); j++) {
Console.Write(array[i, j] + " ");
}
Console.WriteLine();
}
}
}
The output of the above code is −
Original Array: 64 34 25 22 11 90 77 30 15 Entire Array Sorted: 11 15 22 25 30 34 64 77 90
Comparison of Approaches
| Method | Time Complexity | Best For |
|---|---|---|
| Nested Loops (Bubble Sort) | O(n³) | Small arrays, educational purposes |
| Array.Sort() with Jagged Arrays | O(n² log n) | Sorting individual rows efficiently |
| LINQ with Flattening | O(n log n) | Sorting entire array as single collection |
Conclusion
Sorting two-dimensional arrays in C# can be done row-wise using nested loops or Array.Sort(), or as a complete collection by flattening first. The Array.Sort() method with jagged arrays provides the most efficient solution for sorting individual rows, while LINQ offers a clean approach for sorting all elements together.
