- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 Articles
- How do I declare a two-dimensional array in C++ using new?
- How do we access elements from the two-dimensional array in C#?
- How to declare a two-dimensional array in C#
- What is a two-dimensional array in C language?
- Passing two dimensional array to a C++ function
- How to create a two dimensional array in JavaScript?
- C# program to Loop over a two dimensional array
- Split one-dimensional array into two-dimensional array JavaScript
- How to perform arithmetic operations on two-dimensional array in C?
- How to sort one dimensional array in descending order using Array Class method?
- Explain pointers and two-dimensional array in C language
- How do I sort natural in MongoDB?
- How do I sort a multidimensional array by one of the fields of the inner array in PHP?
- How do you sort an array in C# in ascending order?
- How do you sort an array in C# in descending order?

Advertisements