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 OverflowUse 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]));
Welcome Java 8:
Arrays.sort(myArr, (a, b) -> Double.compare(a[0], b[0]));
Sorting 2D arrays by rows in Java using comparators
How to sort two dimensional array in Java? - Data Science Stack Exchange
Understanding Java Sorting a 2D-Array using Arrays.sort() - Stack Overflow
Sorting a 2D array, help.
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!