Sort 2D Array in Java
-
Use
java.util.Arrays.sort(T[] a, Comparator<? super T> c)
to Sort a 2D Array Given Column Wise -
Use
java.util.Arrays.sort(T[] a)
to Sort 2D Array Row-Wise

In this tutorial, we will learn how to sort a 2D array in Java. A 2D array is an array whose elements are 1-D arrays. The elements in a 2D array are arranged in rows and columns in the form of a matrix.
Use java.util.Arrays.sort(T[] a, Comparator<? super T> c)
to Sort a 2D Array Given Column Wise
java.util.Arrays.sort(T[] a, Comparator<? super T> c)
is one of the few utility methods provided by Arrays
class. This method sorts the specified array according to the order actuated by the specified comparator. The elements in the array must be mutually comparable hence should not throw a ClassCastException
.
Here multi
is a 2-dimensional array in matrix form that has three rows and three columns. We will sort this array taking its third column as reference.
We pass an implementation of the java.util.Comparator
interface to the Arrays.sort()
method. The Comparator
interface defines a compare
method that compares its two arguments for order. It returns zero if the passed objects are equal. It returns a positive value if first[columnNumber-1]
is greater than second[columnNumber-1]
. Otherwise, it returns a negative value.
The third column of multi
has 8, 2, and 6 as the elements. It compares these values and alters the order of the 2D array in ascending order.
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();
}
//sort according to 3 column
Sort2DArrayBasedOnColumnNumber(multi,3);
System.out.println("after sorting");
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
7 5 2
3 0 6
4 9 8
Use java.util.Arrays.sort(T[] a)
to Sort 2D Array Row-Wise
Here in the code, array[][]
is a 2D array. It has 4 rows and 4 columns. We will sort the individual row of this array using the Arrays.sort()
method that takes an array as the argument. This method sorts the specified array into ascending numerical order.
The sortRowWise
method runs a for
loop till the length of our array. It takes an individual row of array[][]
and sorts it in ascending order, shown in the output.
import java.util.Arrays;
public class Sort2DArray {
public static void main(String args[]) {
int array[][] = {{7, 8, 2, 1},
{0, 3, 2, 9},
{6, 5, 3, 2},
{8, 3, 7, 9}};
sortRowWise(array);
}
static int sortRowWise(int arr[][]) {
// One by one sort individual rows.
for (int i = 0; i < arr.length; i++) {
Arrays.sort(arr[i]);
}
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++)
System.out.print(arr[i][j] + " ");
System.out.println();
}
return 0;
}
}
Output:
1 2 7 8
0 2 3 9
2 3 5 6
3 7 8 9
Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.
LinkedInRelated Article - Java Array
- Concatenate Two Arrays in Java
- Convert Byte Array in Hex String in Java
- Remove Duplicates From Array in Java
- Count Repeated Elements in an Array in Java
- Natural Ordering in Java
- Slice an Array in Java