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

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 the 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
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

The Idea is to use Arrays.sort in Java.

 `// Java Code to sort 2D Matrix ` `// according to any Column ` `import` `java.util.*; ` `class` `sort2DMatrixbycolumn { ` ` `  `    ``// Function to sort by column ` `    ``public` `static` `void` `sortbyColumn(``int` `arr[][], ``int` `col) ` `    ``{ ` `        ``// Using built-in sort function Arrays.sort ` `        ``Arrays.sort(arr, ``new` `Comparator<``int``[]>() { ` `           `  `          ``@Override`               `          ``// Compare values according to columns ` `          ``public` `int` `compare(``final` `int``[] entry1,  ` `                             ``final` `int``[] entry2) { ` ` `  `            ``// To sort in descending order revert  ` `            ``// the '>' Operator ` `            ``if` `(entry1[col] > entry2[col]) ` `                ``return` `1``; ` `            ``else` `                ``return` `-``1``; ` `          ``} ` `        ``});  ``// End of function call sort(). ` `    ``} ` `     `  `    ``// Driver Code ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``int` `matrix[][] = { { ``39``, ``27``, ``11``, ``42` `}, ` `                           ``{ ``10``, ``93``, ``91``, ``90` `}, ` `                           ``{ ``54``, ``78``, ``56``, ``89` `}, ` `                           ``{ ``24``, ``64``, ``20``, ``65` `} }; ` `        ``// Sort this matrix by 3rd Column ` `        ``int` `col = ``3``; ` `        ``sortbyColumn(matrix, col - ``1``); ` ` `  `        ``// Display the sorted Matrix ` `        ``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
```

Time complexity : O(n Log n) where n is number of rows. Here assumption is that the sort() function uses a O(n Log n) sorting algorithm.

This article is contributed by DANISH KALEEM. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up
Article Tags :
Practice Tags :

5

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.