0

I guess this is kind of a common question, but I looked at some other responses and couldn't get my code to work.

I have a 2d array of doubles, two columns and a bunch of rows, and I want to sort the array by the first column in ascending order.

My array is called ratio[][], and I tried this code:

  Arrays.sort(ratio, new Comparator<double[]>() {

    @Override
    public double compare(double[] o1, double[] o2) {
        return valueOf(o1[0]).compareTo(valueOf(o2[0]));
    }

  });

The compiler is finding a bunch of errors. I've imported java.util.Arrays and java.util.Comparator. What am I doing wrong?

1
  • You are just comparing two double values again and again, not array of doubles. Commented Oct 30, 2012 at 5:12

3 Answers 3

3

If you want to define the ordering of your double arrays by the first element (which seems like what you are trying to do), then all you need to do is:

Arrays.sort(ratio, new Comparator<double[]>() {
    @Override
    public double compare(double[] o1, double[] o2) {
        return Double.compare(o1[0], o2[0]);
    }
});

There's no need to construct a Double object for each comparison (or to convert your arrays to type Double[]).

Sign up to request clarification or add additional context in comments.

Comments

2

Your valueOf is a static method of Double class. It should be invoked on it.

If you are just sorting the 2-D array on the basis of first column of each array, it would be like this: -

Arrays.sort(ratio, new Comparator<double[]>() {
        @Override
        public int compare(double[] o1, double[] o2) {
            return Double.valueOf(o1[0]).compareTo(Double.valueOf(o2[0]));
        }
});

And, you can use Comparator on type double[] since an array is an object only. So, you don't need to convert it to Double[][].

UPDATE: -

  • You can also use Double.compare(double, double) method as in @Ted's answer, because that will not need you to create two double objects. But, that's not going to matter much on small scale, if you are building a large application having lots of comparison, then you should definitely go this one.

15 Comments

That did it, I'm surprised I was that close... Thank you!
Maybe I'm missing the point, but this won't sort the array. Have a look at my answer for examples (yes, I just tried your answer it still won't sort the array)
@MadProgrammer. I want to sort the array by the first column in ascending order. This is what OP wants. Now you can try the code and see whether it does it. I think you misunderstood the question. Even I got it wrong the first time. :)
@RohitJain I am missing something, sleep. I've being trying to sort the inner array not the outer :P
No worries about trying to use compareTo on primitives. :) Regarding your edit--using o1[0] - o2[0] has several defects: it can overflow; it can fall victim to catastrophic round-off errors; it does not correctly handle differences less than 1; and it does not correctly handle NaN values. Much better to use Double.compare(double,double).
|
0

just a suggestion..

  class Row {
      public double one;
      public double two;
  }  

  Row[] rows = // create this

  Arrays.sort(rows, new Comparator<Row>() {
      public int compare(Row o1, Row o2) {
          double c = o1.one - o2.one;
          return c < 0.0 ? -1 : c > 0.0 ? 1 : 0;
      }
  }

12 Comments

This can have a problem with underflow/overflow and does not correctly handle NaN.
Ahhh... Also, if MadProgrammer's answer works I retract my answer :)
@catchpolenet. No, his answer is not going to work. It will throw an Exception at runtime.
@catchpolenet. You're very first line is incorrect. We can create Comparator on double[]. An array is an object right?
@RohitJain Apart from sorting the wrong array index (:P), why would it have thrown an exception?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.