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
🌐
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
Discussions

Sorting 2D arrays by rows in Java using comparators
This is lambda expression in Java. Basically what it does is just sort two arrays by their first element More on reddit.com
🌐 r/leetcode
2
2
July 23, 2021
How to sort two dimensional array in Java? - Data Science Stack Exchange
I have a two dimensional double array. I need to sort my based column 1 in descending order. How can I sort my two dimensional array in descending order. Sample data and code are the following: More on datascience.stackexchange.com
🌐 datascience.stackexchange.com
November 11, 2018
Understanding Java Sorting a 2D-Array using Arrays.sort() - Stack Overflow
The algorithm will consult the Comparator as many times as needed, using the results of the comparisons to determine which elements need to be moved, until the array is sorted. A 2D array is essentially just an array of arrays. More on stackoverflow.com
🌐 stackoverflow.com
Sorting a 2D array, help.
I might be wrong about this buuuuut, if you interpreted the 2d array as a one dimensional array in row major order, the problem is reduced to sorting a one dimensional array. For example a 5x5 array could be copied to a 25 1d array, sorted, then copied back to the 5x5 in the right order. More on reddit.com
🌐 r/javahelp
6
2
October 9, 2015
🌐
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.
🌐
Reddit
reddit.com › r/leetcode › sorting 2d arrays by rows in java using comparators
r/leetcode on Reddit: Sorting 2D arrays by rows in Java using comparators
July 23, 2021 -

So I was doing the merge interval question and came across this in the solutions

link: https://leetcode.com/problems/merge-intervals/solution/

Arrays.sort(intervals, (arr1,arr2) -> Integer.compare(arr1[0],arr2[0]));

How does this work? I am not too familiar with comparators but it reminds me of how we instantiate a max heap. Also, why are we passing arr1[0],arr2[0], seems arbitrary

Appreciate any advice, Thanks!

🌐
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); ...
🌐
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)); } } }
🌐
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
Find elsewhere
🌐
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 in ascending and descending order.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
TutorialsPoint
tutorialspoint.com › article › 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.
🌐
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
🌐
Quora
quora.com › How-do-I-sort-a-2D-array-column-wise-in-Java-Is-there-an-easy-way
How to sort a 2D array column-wise in Java? Is there an easy way - Quora
Answer: // 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 () { ...
🌐
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.
🌐
BluejCode
bluejcode.wixsite.com › tech › post › sort-2d-array
Sort 2-D Array in Java
May 1, 2019 - import java.io.*; import java.util.*; class Sort { public static void main(String args[]) { int n=0,t,i,j; Scanner in= new Scanner(System.in); int ar[][]=new int[3][3]; // Array Defined int br[]=new int[9]; // 1-D Array to Store 2D array elements int cr[][]=new int[3][3]; // 2D Array to store sorted data System.out.println("Enter Array Elements"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { ar[i][j]= in.nextInt(); // Input Array Elements } } for(i=0;i<3;i++) { for(j=0;j<3;j++) { br[n++]=ar[i][j]; // Storing 2D elements in 1D array } } for(i=0;i<9;i++) // These two loops are used to Sort Array Elemen
🌐
TutorialsPoint
tutorialspoint.com › article › 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); } }
🌐
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 - 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 ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-sort-the-2d-array-across-rows
Java Program to Sort the 2D Array Across Rows - GeeksforGeeks
September 15, 2021 - // Java Program to Sort the 2D array Across Rows // Importing required libraries import java.io.*; import java.lang.*; import java.util.*; // Main class public class GFG { // Main driver method public static void main(String[] args) throws java.lang.Exception { // Custom input 2D matrix int[][] arr = { { 1, 8, 4, 7, 3 }, { 8, 3, 1, 7, 5 }, { 6, 2, 0, 7, 1 }, { 2, 6, 4, 1, 9 } }; // Display message only System.out.println("Matrix without sorting \n"); // Print and display the matrix before sorting // using nested for loops for (int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) { // Printing
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-sort-2d-array-across-columns
Java Program to Sort 2D Array Across Columns - GeeksforGeeks
July 23, 2025 - // Java Program to Sort 2D array across Columns // Importing required classes import java.io.*; import java.lang.*; import java.util.*; // Main class class GFG { // Main driver method public static void main(String[] args) throws java.lang.Exception { // Custom input for 2D array int[][] arr = { { 7, 2, 0, 5, 1 }, { 3, 8, 2, 9, 14 }, { 5, 1, 0, 5, 2 }, { 4, 2, 6, 0, 1 } }; // Display message for better readability System.out.println("Matrix without sorting \n"); // Nested iteration to display matrix for (int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) { // Printing elements of 2D matrix S