Question

# [Solved] java Arrays.sort 2d array

I am looking to sort the following array based on the values of []

``````double[][] myArr = new double[mySize];
``````

so for example, myArr contents is:

``````1      5
13     1.55
12     100.6
12.1   .85
``````

I want it to get to:

``````1      5
12     100.6
12.1   .85
13     1.55
``````

I am looking to do this without having to implement my own sort.

## Solution #1:

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, b);
}
});
``````

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

``````Arrays.sort(array, Comparator.comparingDouble(o -> o));
``````

## Solution #2:

Welcome Java 8:

``````Arrays.sort(myArr, (a, b) -> Double.compare(a, b));
``````

## Solution #3:

The simplest way:

``````Arrays.sort(myArr, (a, b) -> a - b);
``````

## Solution #4:

You need to implement a `Comparator<Double[]>` like so:

``````public static void main(String[] args) throws IOException {
final Double[][] doubles = new Double[][]{{5.0, 4.0}, {1.0, 1.0}, {4.0, 6.0}};
final Comparator<Double[]> arrayComparator = new Comparator<Double[]>() {
@Override
public int compare(Double[] o1, Double[] o2) {
return o1.compareTo(o2);
}
};
Arrays.sort(doubles, arrayComparator);
for (final Double[] arr : doubles) {
System.out.println(Arrays.toString(arr));
}
}
``````

Output:

``````[1.0, 1.0]
[4.0, 6.0]
[5.0, 4.0]
``````

## Solution #5:

Although this is an old thread, here are two examples for solving the problem in Java8.

sorting by the first column ([]):

``````double[][] myArr = new double[mySize];
// ...
java.util.Arrays.sort(myArr, java.util.Comparator.comparingDouble(a -> a));
``````

sorting by the first two columns ([], []):

``````double[][] myArr = new double[mySize];
// ...
java.util.Arrays.sort(myArr, java.util.Comparator.<double[]>comparingDouble(a -> a).thenComparingDouble(a -> a));
``````

## Solution #6:

Decreasing/increasing order for an integer array of 2 dimension you can use:

``````Arrays.sort(contests, (a, b) -> Integer.compare(b,a)); //decreasing order

Arrays.sort(contests, (a, b) -> Integer.compare(a,b); //increasing order
``````

## Solution #7:

Simplified Java 8

IntelliJ suggests to simplify the top answer to the:

``````Arrays.sort(queries, Comparator.comparingDouble(a -> a));
``````

## Solution #8:

To sort in descending order you can flip the two parameters

``````int[][] array= {
{1, 5},
{13, 1},
{12, 100},
{12, 85}
};
Arrays.sort(array, (b, a) -> Integer.compare(a, b));
``````

Output:

``````13, 5
12, 100
12, 85
1, 5
``````

## Solution #9:

much simpler code:

import java.util.Arrays;
int[][] array = new int[][];

Arrays.sort(array, ( a, b) -> a – b);

## Solution #10:

It is really simple, there are just some syntax you have to keep in mind.

Arrays.sort(contests, (a, b) -> Integer.compare(a,b));//increasing order —1

Arrays.sort(contests, (b, a) -> Integer.compare(b,a));//increasing order —2

Arrays.sort(contests, (a, b) -> Integer.compare(b,a));//decreasing order —3

Arrays.sort(contests, (b, a) -> Integer.compare(a,b));//decreasing order —4

If you notice carefully, then it’s the change in the order of ‘a’ and ‘b’ that affects the result. For line 1, the set is of (a,b) and Integer.compare(a,b), so it is increasing order. Now if we change the order of a and b in any one of them, suppose the set of (a,b) and Integer.compare(b,a) as in line 3, we get decreasing order.

## Solution #11:

For a general solution you can use the Column Comparator. The code to use the class would be:

``````Arrays.sort(myArr, new ColumnComparator(0));
``````

## Solution #12:

``````import java.util.*;

public class Arrays2
{
public static void main(String[] args)
{
int small, row = 0, col = 0, z;
int[][] array = new int;

Random rand = new Random();
for(int i = 0; i < array.length; i++)
{
for(int j = 0; j < array[i].length; j++)
{
array[i][j] = rand.nextInt(100);
System.out.print(array[i][j] + " ");
}
System.out.println();
}

System.out.println("
");

for(int k = 0; k < array.length; k++)
{
for(int p = 0; p < array[k].length; p++)
{
small = array[k][p];
for(int i = k; i < array.length; i++)
{
if(i == k)
z = p + 1;
else
z = 0;
for(;z < array[i].length; z++)
{
if(array[i][z] <= small)
{
small = array[i][z];
row = i;
col = z;
}
}
}
array[row][col] = array[k][p];
array[k][p] = small;
System.out.print(array[k][p] + " ");
}
System.out.println();
}
}
}
``````

Good Luck

## Solution #13:

Java 8 is now very common nowadays.

``````Arrays.sort(myArr,(double[] a,double[] b)->{
//here multiple lines of code can be placed
return a-b;
});
``````

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .
To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy