 
 Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP 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
Java Program to Sort 2D Array Across Left Diagonal
For a given 2D array, our task is to write a Java program to sort its element present across the left diagonal. For this problem, we need to arrange the elements of the array in such a way that the values are sorted when read from the top left to the bottom right, which is the left diagonal of the array.
Example Scenario:
Input: matrix = {3, 8, 2}, {6, 5, 4}, {3, 2, 1};
Output: new_matrix = {1, 4, 2}, {2, 3, 8}, {3, 6, 5}
Use the below sorting tool to understand the problem:
| 4 | 9 | 8 | 
| 2 | 2 | 7 | 
| 6 | 5 | 5 | 
Left Diagonal Sorting of 2D Array in Ascending Order
The idea here is to find the length of diagonal, which is the minimum of the remaining rows and columns from the starting point, and to create an array that stores diagonal elements. Then, we can sort this array using Arrays.sort() method to get diagonal elements in ascending order.
Example
Let's see the practical demonstration ?
import java.util.Arrays;
public class DgnlSortPrgrm {
   public static void main(String[] args) {
      int[][] twoDArr = {
         {3, 8, 2},
         {6, 5, 4},
         {3, 2, 1}
      };
      System.out.println("Matrix without sorting its diagonal::");
      printMatrix(twoDArr);
      // calling method to sort
      sorting(twoDArr);
      System.out.println("Matrix after sorting its diagonal::");
      printMatrix(twoDArr);
   }
   // method to sort diagonals
   public static void sorting(int[][] twoDArr) {
      int n = twoDArr.length;
      int m = twoDArr[0].length;
      // Sort diagonal that starts from the first row
      for (int col = 0; col < m; col++) {
         sortDgnlElem(twoDArr, 0, col);
      }
      // Sort diagonal that starts from the first column
      for (int row = 1; row < n; row++) {
         sortDgnlElem(twoDArr, row, 0);
      }
   }
   // method to sort elements of the diagonal
   private static void sortDgnlElem(int[][] twoDArr, int row, int col) {
      int n = twoDArr.length;
      int m = twoDArr[0].length;
      int len = Math.min(n - row, m - col);
      int[] dgnl = new int[len];
      // getting diagonal elements
      for (int i = 0; i < len; i++) {
         dgnl[i] = twoDArr[row + i][col + i];
      }
      // sorting diagonal elements
      Arrays.sort(dgnl);
      // sorted elements of the matrix
      for (int i = 0; i < len; i++) {
         twoDArr[row + i][col + i] = dgnl[i];
      }
   }
   // method to print the matrix
   private static void printMatrix(int[][] matrix) {
      for (int[] row : matrix) {
         for (int elem : row) {
            System.out.print(elem + " ");
         }
         System.out.println();
      }
   }
}
On running this code, you will get the following result ?
Matrix without sorting its diagonal:: 3 8 2 6 5 4 3 2 1 Matrix after sorting its diagonal:: 1 4 2 2 3 8 3 6 5
Left Diagonal Sorting of 2D Array in Descending Order
In this approach, we follow the same logic with minor changes. Here, we first sort the elements of the diagonal and then reverse their order.
Example
This Java program demonstrates how to sort 2D array across left diagonal in descending order.
import java.util.Arrays;
public class DgnlSortPrgrm {
   public static void main(String[] args) {
      int[][] twoDArr = {
         {3, 8, 2},
         {6, 5, 4},
         {3, 2, 1}
      };
      System.out.println("Matrix without sorting its diagonal::");
      printMatrix(twoDArr);
      // calling method to sort
      sorting(twoDArr);
      System.out.println("Matrix after sorting its diagonal::");
      printMatrix(twoDArr);
   }
   // method to sort diagonals
   public static void sorting(int[][] twoDArr) {
      int n = twoDArr.length;
      int m = twoDArr[0].length;
      // Sort diagonal that starts from the first row
      for (int col = 0; col < m; col++) {
         sortDgnlElem(twoDArr, 0, col);
      }
      // Sort diagonal that starts from the first column
      for (int row = 1; row < n; row++) {
         sortDgnlElem(twoDArr, row, 0);
      }
   }
   // method to sort elements of the diagonal
   private static void sortDgnlElem(int[][] twoDArr, int row, int col) {
      int n = twoDArr.length;
      int m = twoDArr[0].length;
      int len = Math.min(n - row, m - col);
      int[] dgnl = new int[len];
      // getting diagonal elements
      for (int i = 0; i < len; i++) {
         dgnl[i] = twoDArr[row + i][col + i];
      }
      // sorting diagonal elements
      Arrays.sort(dgnl);
      // reversing the sorted array for descending order
      for (int i = 0; i < len / 2; i++) {
         int temp = dgnl[i];
         dgnl[i] = dgnl[len - i - 1];
         dgnl[len - i - 1] = temp;
      }
      // sorted elements of the matrix
      for (int i = 0; i < len; i++) {
         twoDArr[row + i][col + i] = dgnl[i];
      }
   }
   // method to print the matrix
   private static void printMatrix(int[][] matrix) {
      for (int[] row : matrix) {
         for (int elem : row) {
            System.out.print(elem + " ");
         }
         System.out.println();
      }
   }
}
When you execute the code, it will show the following output ?
Matrix without sorting its diagonal:: 3 8 2 6 5 4 3 2 1 Matrix after sorting its diagonal:: 5 8 2 6 3 4 3 2 1
