Deep Dive: Java 2d Array Sort

In this blog we will take a look as to how to sort 2D array in java with various examples

Let’s get started!

Sorting 2d array based on 1 column or 1st element

Let us declare a simple 2d array.

To sort this array we need to use Arrays.sort() method by passing it right comparator which compares only the first element of each array for sorting

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
import java.util.Arrays;

public class InstantDemo {


    public static void main(String[] args) {

        int[][] array2D = {{5, 3,6}, {2, 8,3}, {7, 1,2}, {4, 6,9}};

        // Sort the 2D array based on the first element of each subarray
        Arrays.sort(array2D, (a, b) -> Integer.compare(a[0], b[0]));

        // Print the sorted 2D array
        for (int[] row : array2D) {
            System.out.println(Arrays.toString(row));
        }

    }
}

Output:

1
2
3
4
        [2, 8, 3]
        [4, 6, 9]
        [5, 3, 6]
        [7, 1, 2]

You can see above 2d array is sorted based on 1st element or 1st column

Sorting 2d array based on 1st column in descending order

You can sort 2d array in descending order as shown below

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
import java.util.Arrays;

public class InstantDemo {


    public static void main(String[] args) {

        int[][] array2D = {{5, 3,6}, {2, 8,3}, {7, 1,2}, {4, 6,9}};

        // Sort the 2D array based on the first element of each subarray
        Arrays.sort(array2D, (a, b) -> Integer.compare(b[0], a[0]));

        // Print the sorted 2D array
        for (int[] row : array2D) {
            System.out.println(Arrays.toString(row));
        }

    }
}
1
2
3
4
[7, 1, 2]
[5, 3, 6]
[4, 6, 9]
[2, 8, 3]

Sorting 2d array based on last element or last column

Similarly you can sort 2d array using any column.

In the below example we sort the array based on last element in the array.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
import java.util.Arrays;

public class InstantDemo {


    public static void main(String[] args) {

        int[][] array2D = {{5, 3,6}, {2, 8,3}, {7, 1,2}, {4, 6,9}};

        // Sort the 2D array based on the last element of each subarray
        Arrays.sort(array2D, (a, b) -> Integer.compare(a[2], b[2]));

        // Print the sorted 2D array
        for (int[] row : array2D) {
            System.out.println(Arrays.toString(row));
        }

    }
}

Output:

1
2
3
4
        [7, 1, 2]
        [2, 8, 3]
        [5, 3, 6]
        [4, 6, 9]

Sorting 2d array based on row/(sum of the elements in the array)

Let us say you want to sort 2d array based on row or sum of all elements in the array.

Looks tricky! But it is very easy if use Arrays.stream(a).sum() which gives sum of all elements in each row.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import java.util.Arrays;

public class InstantDemo {


    public static void main(String[] args) {

        int[][] array2D = {{5, 3,6}, {2, 8,3}, {7, 1,2}, {4, 6,9}};

        // Sort the 2D array based on the sum of each subarray
        Arrays.sort(array2D, (a, b) ->
                Integer.compare(Arrays.stream(a).sum(), Arrays.stream(b).sum()));

        // Print the sorted 2D array
        for (int[] row : array2D) {
            System.out.println(Arrays.toString(row));
        }

    }
}
1
2
3
4
[7, 1, 2]  --> 10
[2, 8, 3]  --> 13
[5, 3, 6]  --> 14
[4, 6, 9]``--> 19`

Java sort 2D array alphabetically

To sort a 2D array alphabetically in Java, you can use the Arrays.sort() method with a custom comparator that compares the elements based on their natural order.

We sort it alphabetically based on the first element of each subarray (column 0). Here use a custom comparator that compares the strings using the compareTo() method, which compares strings based on their natural order.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.Arrays;

public class InstantDemo {


    public static void main(String[] args) {

        String[][] array2D = {
                {"Lemon", "Kiwi", "Orange"},
                {"Banana", "Apple", "Cherry"},
                {"Grape", "Fig", "Date"}

        };

        // Sort the 2D array alphabetically
        Arrays.sort(array2D,(a,b) -> a[0].compareTo(b[0]));

        // Print the sorted 2D array
        for (String[] row : array2D) {
            System.out.println(Arrays.toString(row));
        }

    }
}
1
2
3
[Banana, Apple, Cherry]
[Grape, Fig, Date]
[Lemon, Kiwi, Orange]