Use Overloaded Arrays#Sort(T[] a, Comparator c) which takes Comparator as the second argument.

double[][] array= {
{1, 5},
{13, 1.55},
{12, 100.6},
{12.1, .85} };

java.util.Arrays.sort(array, new java.util.Comparator<double[]>() {
    public int compare(double[] a, double[] b) {
        return Double.compare(a[0], b[0]);
    }
});

JAVA-8: Instead of that big comparator, we can use lambda function as following-

Arrays.sort(array, Comparator.comparingDouble(o -> o[0]));
Answer from PermGenError on Stack Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 26748842 › how-to-display-in-descending-order-a-two-dimensional-array
java - how to display in descending order a two dimensional array - Stack Overflow
May 25, 2017 - public static void main(String[] args) { double[][] employeeWorkHours = { { 2, 4, 3, 4, 5, 8, 8 }, { 7, 3, 4, 3, 3, 4, 4 }, { 3, 3, 4, 3, 3, 2, 2 }, { 9, 3, 4, 7, 3, 4, 1 }, { 3, 5, 4, 3, 6, 3, 8 }, { 3, 4, 4, 6, 3, 4, 4 }, { 3, 7, 4, 8, 3, 8, 4 }, { 6, 3, 5, 9, 2, 7, 9 } }; Employee[] totals = new Employee[employeeWorkHours.length]; for (int i = 0; i < employeeWorkHours.length; i++) { totals[i] = new Employee(i, sumRow(employeeWorkHours[i])); } Arrays.sort(totals); for (Employee e : totals) { System.out.println(e); } }
🌐
GeeksforGeeks
geeksforgeeks.org › java › sorting-2d-array-according-values-given-column-java
Sorting a 2D Array according to values in any given column in Java - GeeksforGeeks
Use Arrays.sort() with a comparator. Compare the values of the selected column in each row. The complete rows are rearranged based on those values. ... import java.io.*; import java.util.*; class GFG { public static void sortbyColumn(int a[][], ...
Published: August 21, 2026
🌐
Java67
java67.com › 2014 › 08 › 4-examples-to-sort-array-in-java.html
7 Examples to Sort One and Two Dimensional String and Integer Array in Java | Ascending, Descending and Reverse Order | Java67
Alternatively, you can copy the source in a text file with the name same as the main class, compile it using the javac command and run it by using the java command directly from the command prompt. This contains code to sort a primitive array on increasing order, sorting integer and string array in ascending and descending order, sorting object arrays, and sorting 2D array columns.
🌐
Stack Overflow
stackoverflow.com › questions › 69640062 › sort-2d-array-elements-in-descending-order
java - Sort 2D array elements in descending order - Stack Overflow
Sorted array in your example is in ascending order, not descending. ... Save this answer. ... Show activity on this post. CopyString tab[][] = new String[][]{{"1","b","g"},{"e","3"},{"r","2","9","a"},{"2"}}; String temp[]; for (int i = 0; i ...
🌐
EDUCBA
educba.com › home › software development › software development tutorials › java tutorial › 2d array sorting in java
2D Array Sorting in Java | Examples of 2D Array Sorting in Java
April 11, 2023 - In a 2D array, a cell has two indexes one is its row number, and the other is its column number. Sorting is a technique for arranging elements in a 2D array in a specific order. The 2D array can be in either ascending or descending order. Let’s see how to sort different ways the 2D array in Java ...
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
TutorialsPoint
tutorialspoint.com › java-program-to-sort-the-2d-array-across-rows
Java Program to Sort the 2D Array Across Rows
July 31, 2023 - Array without sorting 1 3 8 5 9 5 3 1 7 4 0 5 9 2 6 4 6 3 1 0 3 5 8 9 2 Array after sorting 1 3 5 8 9 1 3 4 5 7 0 2 5 6 9 0 1 3 4 6 2 3 5 8 9 · Above is the output of the vector() method where we have printed the unsorted matrix first and after the program is executed, the sorted matrix is displayed. import java.io.*; import java.util.Arrays; public class ArraySort { static int sortrowWise ( int arr [][]) { 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; } public static void main(String args[]){ int arr[][] = { { 9,2,6,4,5}, { 8,3,7,0,2}, { 5,3,8,1,2}, { 3,5,7,1,0} }; sortrowWise(arr); } }
Find elsewhere
🌐
YouTube
youtube.com › watch
Java program to sort 2d array in ascending order. - YouTube
In this Java Program i show how to sort a 2d array or matrix in ascending order it can be reversed in descending order. Sort 2d array in ascending order.I ...
Published: October 15, 2020
🌐
codippa
codippa.com › home › how to sort a two-dimensional array on column values in java / how to sort a two-dimensional array in java
How to sort a two-dimensional array on column values in java / How to sort a two-dimensional array in java - codippa
January 13, 2025 - For sorting the array, sort method of java.util.Arrays is used which takes 2 arguments : the array to be sorted and a java.util.Comparator object. We pass an anonymous Comparator object(anonymous object means an object which has no name and which is created at its place of use only). As explained earlier and can be seen from Illustration 1, a 2d array in java is an array of arrays, thus, for sorting a 2d array on a column, we have to sort an integer array, therefore the generic type of this Comparator object should be Integer[ ]. Inside Comparator object, we implement its compare method which takes two objects of Integer[ ] type.
Top answer
1 of 8
81

Sort a two dimensional array based on one column
The first column is a date of format "yyyy.MM.dd HH:mm" and the second column is a String.

Since you say 2-D array, I assume "date of format ..." means a String. Here's code for sorting a 2-D array of String[][]:

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

public class Asdf {

    public static void main(final String[] args) {
        final String[][] data = new String[][] {
                new String[] { "2009.07.25 20:24", "Message A" },
                new String[] { "2009.07.25 20:17", "Message G" },
                new String[] { "2009.07.25 20:25", "Message B" },
                new String[] { "2009.07.25 20:30", "Message D" },
                new String[] { "2009.07.25 20:01", "Message F" },
                new String[] { "2009.07.25 21:08", "Message E" },
                new String[] { "2009.07.25 19:54", "Message R" } };

        Arrays.sort(data, new Comparator<String[]>() {
            @Override
            public int compare(final String[] entry1, final String[] entry2) {
                final String time1 = entry1[0];
                final String time2 = entry2[0];
                return time1.compareTo(time2);
            }
        });

        for (final String[] s : data) {
            System.out.println(s[0] + " " + s[1]);
        }
    }

}

Output:

2009.07.25 19:54 Message R
2009.07.25 20:01 Message F
2009.07.25 20:17 Message G
2009.07.25 20:24 Message A
2009.07.25 20:25 Message B
2009.07.25 20:30 Message D
2009.07.25 21:08 Message E
2 of 8
12
class ArrayComparator implements Comparator<Comparable[]> {
    private final int columnToSort;
    private final boolean ascending;

    public ArrayComparator(int columnToSort, boolean ascending) {
        this.columnToSort = columnToSort;
        this.ascending = ascending;
    }

    public int compare(Comparable[] c1, Comparable[] c2) {
        int cmp = c1[columnToSort].compareTo(c2[columnToSort]);
        return ascending ? cmp : -cmp;
    }
}

This way you can handle any type of data in those arrays (as long as they're Comparable) and you can sort any column in ascending or descending order.

String[][] data = getData();
Arrays.sort(data, new ArrayComparator(0, true));

PS: make sure you check for ArrayIndexOutOfBounds and others.

EDIT: The above solution would only be helpful if you are able to actually store a java.util.Date in the first column or if your date format allows you to use plain String comparison for those values. Otherwise, you need to convert that String to a Date, and you can achieve that using a callback interface (as a general solution). Here's an enhanced version:

class ArrayComparator implements Comparator<Object[]> {
    private static Converter DEFAULT_CONVERTER = new Converter() {
        @Override
        public Comparable convert(Object o) {
            // simply assume the object is Comparable
            return (Comparable) o;
        }
    };
    private final int columnToSort;
    private final boolean ascending;
    private final Converter converter;


    public ArrayComparator(int columnToSort, boolean ascending) {
        this(columnToSort, ascending, DEFAULT_CONVERTER);
    }

    public ArrayComparator(int columnToSort, boolean ascending, Converter converter) {
        this.columnToSort = columnToSort;
        this.ascending = ascending;
        this.converter = converter;
    }

    public int compare(Object[] o1, Object[] o2) {
        Comparable c1 = converter.convert(o1[columnToSort]);
        Comparable c2 = converter.convert(o2[columnToSort]);
        int cmp = c1.compareTo(c2);
        return ascending ? cmp : -cmp;
    }

}

interface Converter {
    Comparable convert(Object o);
}

class DateConverter implements Converter {
    private static final DateFormat df = new SimpleDateFormat("yyyy.MM.dd hh:mm");

    @Override
    public Comparable convert(Object o) {
        try {
            return df.parse(o.toString());
        } catch (ParseException e) {
            throw new IllegalArgumentException(e);
        }
    }
}

And at this point, you can sort on your first column with:

Arrays.sort(data, new ArrayComparator(0, true, new DateConverter());

I skipped the checks for nulls and other error handling issues.

I agree this is starting to look like a framework already. :)

Last (hopefully) edit: I only now realize that your date format allows you to use plain String comparison. If that is the case, you don't need the "enhanced version".

🌐
Oracle
forums.oracle.com › ords › apexds › post › sorting-a-two-dimensional-array-in-descending-order-1586
Sorting a two dimensional array in descending order.. - Oracle Forums
May 12, 2008 - Guy's I'm a beginning Java programmer. I've been struggling with finding documentation on sorting a two dimensional primitive array in descending order.. The following is my code which goes works, in ...
🌐
Edureka Community
edureka.co › home › community › categories › java › how to sort 2d array in java
how to sort 2d array in java | Edureka Community
November 29, 2023 - 260402/how-to-sort-2d-array-in-java · import java.util.Arrays; public class Sort { ...READ MORE
🌐
Medium
medium.com › @it_it_ayush › sorting-a-2d-array-without-row-or-column-order-in-java-466acb9b2cf0
Sorting a 2D-Array Without Row or Column Order in JAVA | by Ayush Gupta | Medium
September 28, 2021 - void sort_array() { for(int i=0; i<a.length;i++) { for(int j=0;j<a[0].length;j++) { List<Integer> l = get_min(i, j); // List with Minimum Value As In Step 1,Step 2, Step 3 if(a[i][j]>l.get(0)) // Check To Prevent Last Value Replacing The First Element { //Simple Swap int t = a[i][j]; a[i][j] = l.get(0); a[l.get(1)][l.get(2)] = t; } } } } Voila! Now You Have An Sorted 2D-Array.
🌐
Delft Stack
delftstack.com › "delft stack" › "howto" › "java howtos" › "how to sort 2d array in java"
How to Sort 2D Array in Java | Delft Stack
February 12, 2024 - This article discusses how to sort a 2D array in Java using two distinct approaches: column-wise sorting using the Comparator interface and row-wise sorting using the Arrays.sort() method.
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › arrays sort in java with examples
How to Sort an Array in Java – Syntax, Methods & Examples
May 19, 2025 - You must define the sorting logic for the inner arrays—commonly based on the first or second element. Java doesn’t support direct sorting of 2D arrays without writing a custom comparator.
🌐
TutorialsPoint
tutorialspoint.com › java-program-to-sort-2d-array-across-columns
Java Program to Sort 2D Array Across Columns
May 31, 2024 - 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.
🌐
BeginnersBook
beginnersbook.com › 2024 › 06 › sorting-2d-array-in-java
Sorting 2D Array in Java
June 3, 2024 - In the following program, we are sorting the given 2D array row-wise, this means that each row of the array needs to be sorted individually. import java.util.Arrays; public class Main { public static void main(String[] args) { // 2D Array int[][] array = { {5, 2, 9}, {1, 3, 7}, {4, 0, 8} }; // Displaying the elements of original array System.out.println("Original array:"); print2DArray(array); // Sort each row of 2D Array sortRows(array); // Displaying the elements of sorted array System.out.println("\nArray sorted row-wise:"); print2DArray(array); } // Method to sort each row of the 2D array public static void sortRows(int[][] array) { for (int[] row : array) { Arrays.sort(row); } } // This method prints the 2D Array public static void print2DArray(int[][] array) { for (int[] row : array) { System.out.println(Arrays.toString(row)); } } }