

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do I sort a two-dimensional array in C#
To sort a two-dimensional array in C#, in a nested for loop, add another for loop to check the following condition.
Example
for (int k = 0; k < j; k++) { if (arr[i, k] > arr[i, k + 1]) { int myTemp = arr[i, k]; arr[i, k] = arr[i, k + 1]; arr[i, k + 1] = myTemp; } }
Till the outer loop loops through, use the GetLength() method as shown below. This is done to sort the array.
Example
for (int i = 0; i < arr.GetLength(0); i++) { for (int j = arr.GetLength(1) - 1; j > 0; j--) { for (int k = 0; k < j; k++) { if (arr[i, k] > arr[i, k + 1]) { int myTemp = arr[i, k]; arr[i, k] = arr[i, k + 1]; arr[i, k + 1] = myTemp; } } } Console.WriteLine(); }
- Related Questions & Answers
- How do I declare a two-dimensional array in C++ using new?
- How do we access elements from the two-dimensional array in C#?
- Split one-dimensional array into two-dimensional array JavaScript
- How to create a two dimensional array in JavaScript?
- How to declare a two-dimensional array in C#
- How do I sort natural in MongoDB?
- Transpose of a two-dimensional array - JavaScript
- Passing two dimensional array to a C++ function
- What is a two-dimensional array in C language?
- Difference Between One-Dimensional (1D) and Two-Dimensional (2D) Array
- Get the Inner product of a One-Dimensional and a Two-Dimensional array in Python
- How do I sort a multidimensional array by one of the fields of the inner array in PHP?
- How to sort one dimensional array in descending order using Array Class method?
- C# program to Loop over a two dimensional array
- How to create JTable from two dimensional array in Java?
Advertisements