Java Program to Sort 2D Array Across Columns


In the field of data structure, the vector is a growable class array of a particular object. The vector class falls in the legacy class which is fully compatible with the collections. In java.util package, the List interface can use all the methods listed here. Here is the initial capacity is 10 and the general method is:

Vector<E> v = new Vector<E>();

The compare() method accepts two parameters and then compare each other by using Java environment logic.

In this article today, we will learn about the sorting process of a 2D array set across the columns.

Algorithm to sort 2D array across columns:-

Here is the particular algorithm to sort the 2D array across columns.

  • Step 1 − Start.

  • Step 2 − Traverse all column one by one.

  • Step 3 − Add elements on that column in the vector.

  • Step 4 − Process those vectors.

  • Step 5 − Sort them again.

  • Step 6 − Push them back from vector to column.

  • Step 7 − Remove that all vectors to make the set empty.

  • Step 8 − Start fresh sorting again.

  • Step 9 − Repeat the all steps again.

  • Step 10 − Complete all the columns like this step by step.

  • Step 11 − Terminate the process

Syntax to sort 2D array across columns

Here we have some particular syntaxes to shot some 2D arrays across columns as follows:

A. removeAll():

Syntax:

Vector.removeAll(Vectors As Value)
It is used to remove all the elements from the vector.

B. Collections.sort():

Syntax:

Collections.sort(Vectors As Value)
This method is used to sort the vector in a process.

C. add():

Syntax:

Vector.add(Value as the integer value)
It is used to add some elements in the vector.

D. get():

Syntax:

Vector.get(3);
This method used to store the vector element at a pricular index.

Here in these particular syntaxes we have tried to sort some 2D arrays across the columns.

Approaches to sort 2D array across columns

  • Approach 1 − Java program to Sort 2D array across columns

  • Approach 2 − Java code to sort 2D Matrix using function Arrays.sort

  • Approach 3 − Java program to sort 2D array same as it is in a 1D array of size n * m using bubble shot

  • Approach 4 − Java program to sort according to 3 column which compares these values and alters the particular order of the 2D array in ascending order

Java program to Sort 2D array across columns

In this Java code we have tried to show how to sort a 2D array across columns in a general manner.

Example 1

import java.io.*;
import java.lang.*;
import java.util.*;

public class ARBRDD {
	public static void main(String[] args)
      throws java.lang.Exception{
      int[][] arr = { { 7, 16, 10, 97, 1 },
         { 3, 8, 2, 9, 14 },
         { 5, 1, 0, 5, 2 },
         { 4, 2, 6, 0, 1 } };
      System.out.println("Matrix without sorting here ----> \n");
      for (int i = 0; i < 4; i++) {
         for (int j = 0; j < 5; j++) {
            System.out.print(arr[i][j] + " ");
      	 }
      	 System.out.println();
      }
      Vector<Integer> v = new Vector<>();

      for (int i = 0; i < 5; i++) {
         for (int j = 0; j < 4; j++) {
            v.add(arr[j][i]);
      	}
      	Collections.sort(v);

      	for (int j = 0; j < 4; j++) {
           arr[j][i] = v.get(j);
      	}
      	v.removeAll(v);
      }

      System.out.println("Matrix after sorting is ----> \n");

      for (int i = 0; i < 4; i++) {
         for (int j = 0; j < 5; j++) {
            System.out.print(arr[i][j] + " ");
      }

      System.out.println();
      }
   }
}

Output

Matrix without sorting here ----> 

7 16 10 97 1 
3 8 2 9 14 
5 1 0 5 2 
4 2 6 0 1 
Matrix after sorting is ----> 

3 1 0 0 1 
4 2 2 5 1 
5 8 6 9 2 
7 16 10 97 14 

Java code to sort 2D Matrix using function Arrays.sort

In this Java code we have tried to show how to sort a 2D array across columns in a general manner by using Arrays.sort method.

Example 2

import java.util.*;
public class sort2DMatrixbycolumn2022 {
   public static void sortbyColumn(int arr[][], int col){
      Arrays.sort(arr, new Comparator<int[]>() {
      	
         @Override      	
         public int compare(final int[] entry1, final int[] entry2) {
            if (entry1[col] > entry2[col])
               return 1;
      	    else
               return -1;
         }
      }); 
   }
   public static void main(String args[]){
      int matrix[][] = { { 39, 27, 11, 42 },
         { 10, 93, 91, 90 },
         { 54, 78, 56, 89 },
         { 24, 64, 20, 65 } };
      int col = 3;
      sortbyColumn(matrix, col - 1);

      for (int i = 0; i < matrix.length; i++) {
      	for (int j = 0; j < matrix[i].length; j++)
           System.out.print(matrix[i][j] + " ");
      	System.out.println();
      }
	}
}

Output

39 27 11 42 
24 64 20 65 
54 78 56 89 
10 93 91 90

Java program to sort 2D array same as it is in a 1D array of size n * m using bubble shot

In this Java code we have tried to show how to sort a 2D array same as it is in a 1D array of size n*m, across columns in a general manner.

Example 3

package jex;
import java.util.*;
public class demo {
   public static void sort(int arr[][]) {
      int i, j, temp;
      int n=arr.length;
      int m=arr[0].length;
      for (i = 0; i < n * m - 1; ++i) {
         for (j = 0; j < n * m - 1 - i; ++j) {
            if (arr[j / m][j % m] > arr[(j + 1) / m][(j + 1) % m]) {
               temp = arr[(j + 1) / m][(j + 1) % m];
               arr[(j + 1) / m][(j + 1) % m] = arr[j / m][j % m];
               arr[j / m][j % m] = temp;
            }
         }
      }
   }
   public static void print(int arr[][]) {
      int i, j;
      int n=arr.length;
      int m=arr[0].length;
      for (i = 0; i < n; ++i) {
         for (j = 0; j < m; ++j) {
            System.out.print(arr[i][j]+" ");
         }
         System.out.println();
      }
   }
   public static void main(String[] args){
      Scanner sc=new Scanner(System.in);
      int[][] arr={ { 5, 12, 17, 12, 23},
         { 1, 2, 4, 6, 8},
         {21, 14, 7, 19, 27},
         { 3, 18, 9, 15, 25}
      };
      System.out.println("Array Before Sorting is here ---->: ");
      print(arr);
      sort(arr);
      System.out.println("Array After Sorting is here ----> : ");
      print(arr);
   }
}

Output

Array Before Sorting is here ---->: 
5 12 17 12 23 
1 2 4 6 8 
21 14 7 19 27 
3 18 9 15 25 
Array After Sorting is here ----> : 
1 2 3 4 5 
6 7 8 9 12 
12 14 15 17 18 
19 21 23 25 27

Java program to sort according to 3 column which compares these values and alters the particular order of the 2D array in ascending order

In this Java code we have tried to show how to sort according to 3 column which compares these values and alters the particular order of the 2D array in ascending order.

Example 4

import java.util.Arrays;
import java.util.Comparator;

public class Sort2DArray {
   public static void main(String args[]) {
      int[][] multi = new int [][]{
         {4, 9, 8},
         {7, 5, 2},
         {3, 0, 6},

      };
      for(int i = 0; i< multi.length; i++) {
         for (int j = 0; j < multi[i].length; j++)
         System.out.print(multi[i][j] + " ");
         System.out.println();

      }
      Sort2DArrayBasedOnColumnNumber(multi,3);
      System.out.println("after sorting we get some value ---->");
      for(int i = 0; i< multi.length; i++) {
         for (int j = 0; j < multi[i].length; j++)
         System.out.print(multi[i][j] + " ");
         System.out.println();

      }
   }
    public static  void Sort2DArrayBasedOnColumnNumber (int[][] array, final int columnNumber){
      Arrays.sort(array, new Comparator<int[]>() {
         @Override
         public int compare(int[] first, int[] second) {
            if(first[columnNumber-1] > second[columnNumber-1]) return 1;
            else return -1;
         }
      });
   }
}

Output

4 9 8 
7 5 2 
3 0 6 
after sorting we get some value ---->
7 5 2 
3 0 6 
4 9 8 

Conclusion

From the above discussion we come to know about the sorting of 2D array problem in details. Today we have used here various sorting methods to solve this problem by the above mentioned syntax and algorithm. Hope with this article you have gained the broad view about how to sort 2D array problem by using a Java environment.

Updated on: 13-Apr-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements