A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/sorting-2d-array-according-values-given-column-java/ below:

Sorting a 2D Array according to values in any given column in Java

Sorting a 2D Array according to values in any given column in Java

Last Updated : 22 Oct, 2024

We are given a 2D array of order N X M and a column number K ( 1<=K<=m). Our task is to sort the 2D array according to values in Column K.

Examples:

Input: If our 2D array is given as (Order 4X4)
39 27 11 42
10 93 91 90
54 78 56 89
24 64 20 65

Sorting it by values in column 3

Output:

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

Java Program to Sort a 2D Array according to values in any given Column Java
// Java Program to Sorting a 2D Array 
// according to values in any given column 
import java.io.*;
import java.util.*;

class GFG {
    public static void sortbyColumn(int a[][], int c){      
      Arrays.sort(a, (x, y) -> Integer.compare(x[c],y[c]));  
    }
  
    public static void main(String args[])
    {
        int m[][] = { { 39, 27, 11, 42 },
                     { 10, 93, 91, 90 },
                     { 54, 78, 56, 89 },
                     { 24, 64, 20, 65 } };
       
      	// Sort this matrix by 3rd Column
      	int c = 3;
      
        sortbyColumn(m, c - 1);
  
        // Display the sorted Matrix
        for (int i = 0; i < m.length; i++) {
          
            for (int j = 0; j < m[i].length; j++)
                System.out.print(m[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 Matrix according to any Column

The idea is to use Arrays.sort in Java.

Syntax:

Arrays.sort(arr, (a,b)->a[0]-b[0]);

Example:

Java
// Java Code to sort 2D Matrix
// according to any Column
import java.util.*;

class sort2DMatrixbycolumn {
    public static void sortbyColumn(int a[][], int c)
    {
      	Arrays.sort(a, new Comparator<int[]>() {
            @Override
            
          	// Compare values according to columns
            public int compare(final int[] entry1,final int[] entry2){
              
                if (entry1[c] > entry2[c])
                    return 1;
                else
                    return -1;
            }
        }); 
    }

  	public static void main(String args[])
    {
        int m[][] = { { 39, 27, 11, 42 },
                     { 10, 93, 91, 90 },
                     { 54, 78, 56, 89 },
                     { 24, 64, 20, 65 } };

        // Sorting the matrix by 3rd Column
        int c = 3;
      
        sortbyColumn(m, c - 1);

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

Output
39 27 11 42 
24 64 20 65 
54 78 56 89 
10 93 91 90 
Explanation of the above Program:

Time complexity: O(n log n) where n is the number of rows.


Sorting a 2D Array according to values in any given column in Java


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4