0

I'm trying to sort a two-dimensional array based on the second column, here is the array:

[1.1, 60.0]
[1.2, 66.66]
[1.3, -1.0]
[1.4, 50.0]
[1.5, 100.0]
[2.1, -1.0]
[2.2, -1.0]
[2.3, -1.0]

I looked into sorting mutli-dimensional arrays and created the following code:

    private void sortKnowledgeScores(double[][] knowledgescores){
    Arrays.sort(knowledgescores, new Comparator<double[]>() {
        @Override
        public int compare(double[] o1, double[] o2) {
            double itemOne = o1[1];
            double itemTwo = o2[1];
            // sort on item id
            if(itemOne < itemTwo){
                return (int) itemOne;
            }
            return (int) itemIdTwo;
        }

This rearranges the array to this:

[2.3, -1.0]
[2.2, -1.0]
[2.1, -1.0]
[1.3, -1.0]
[1.1, 60.0]
[1.2, 66.66]
[1.4, 50.0]
[1.5, 100.0]

Which is correct for the first three values however it puts 50 below 60 and 60.66. How can I adjust my sortKnowledgeScores function to sort my array based on the first column in ascending order?

3 Answers 3

3

Comparator should return 1,0,-1 (as bigger, equel, less).

Generally, if the compare function return positive result the first element is bigger, 0 they equal and negative result mean the second obj is bigger.

You returning the values and not the compare result.

You can look at this doc

You need to change your code to:

public int compare(double[] o1, double[] o2) {
        return (int) o1[1] - o2[1];
 }

As @Mick point out - notice that index you using: index 0 is for the first column and index 1 is for the second

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

3 Comments

o1[0] - o2[0] , I guess
that depend on the OP - I know he said "first" col which mean index 0 but on the code and the explain he talk about the second col so I keep the 1 index here. That is good comment if he actually mean that but I don't think this was his issue
That's my bad, I meant the second column which would have an index of 1
2

First of all, you're using the wrong index. The first element in an array is at index 0

You can use the Comparator-Factory Comparator.comparingDouble:

Comparator.comparingDouble(array -> array[0])
Arrays.sort(knowledgescores, Comparator.comparingDouble(array -> array[0]))

Comments

0

You compare the elements with index "1". This translates to the second column. You should use index "0" if sorting should consider the first column.

Plus - as already mentioned - the compare method works like this:

Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

Comments

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.