Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

I want to sort the array based on the values of [][0]. 

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

Let’s say, below is the content of myArr :

1      5

13     1.55

12     100.6

12.1   .85

I want to sort it like below:

1      5

12     100.6

12.1   .85

13     1.55

Can anyone tell me how to do it?

1 Answer

0 votes
by (19.7k points)

You can use Overloaded Arrays#Sort(T[] a, Comparator c) . Here you’ve to provide a 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]);

    }

});

Interested in Java? Check out this Java Certification by Intellipaat.  

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked May 4, 2021 in Java by sheela_singh (9.5k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...