0

In an ArrayList I have three double[ ] arrays that I wish to sort on the third array. The third array is also unsorted.

public class LevelList extends ArrayList<double[]> {

My comparator compares doubles:

import java.util.Comparator;

public class Linearize {
     static final Comparator<double[]> RATIO_ORDER = 
             new Comparator<double[]>() {
                 @Override
                 public int compare(double[] d1, double[] d2) {
                     //Sort the ratios based on the fraction column 2
                     return Double.compare(d1[2], d2[2]);
                 }
         };
}

I then call:

Collections.sort(levelList, Linearize.RATIO_ORDER);
levelList.println();

However, this only results in a sorting of the order of arrays in ArrayList.

In pseudocode, what I wish to achieve is:

For Each Row of ArrayList at Index i
Sort on Array 3

So that this input:

[1.0][2.0][2.1]
[2.0][5.0][2.2]
[1.0][5.0][1.0]

becomes:

[1.0][5.0][1.0]
[1.0][2.0][2.1]
[2.0][5.0][2.2]

because: 2.2 > 2.1 > 1.0

6
  • what's the desired output? Commented Apr 3, 2015 at 9:59
  • I have demonstrated this under "becomes:" above. Each array should be presented in a column, sorted on the third. Commented Apr 3, 2015 at 10:06
  • I thought that was the output you already got. Commented Apr 3, 2015 at 10:07
  • What I got is actually Col1 Col2 Col3 sorts to Col2 Col3 Col1, I guess depending on each column value. Commented Apr 3, 2015 at 10:08
  • Why extends ArrayList? Avoid extending classes from the collections framework. Commented Apr 3, 2015 at 10:12

1 Answer 1

1

I got the desired output. I tested the following code. Did not understand why levellist has to extend arraylist, hence removed that.

public class TestLevelSorting {
public static void main(String[] args) {

    List<double[]> levelList = new ArrayList<double[]>();

    double[] items1 = {1.0, 2.01, 2.1};
    double[] items2 = {2.0, 5.0, 2.2};
    double[] items3 = {1.0, 5.0, 1.0};

    levelList.add(items1);
    levelList.add(items2);
    levelList.add(items3);

    Collections.sort(levelList, Linearize.RATIO_ORDER);

    for(double[] item : levelList){
        System.out.println(Arrays.toString(item));
    }
}
}

class Linearize {
static final Comparator<double[]> RATIO_ORDER = new Comparator<double[]>() {
    @Override
    public int compare(double[] d1, double[] d2) {
        // Sort the ratios based on the fraction column 2
        return Double.compare(d1[2], d2[2]);
    }
};
}
Sign up to request clarification or add additional context in comments.

2 Comments

This works for the example. However, the actual arrays are much longer than in the example. When inserting the actual arrays, the output looks transposed. I am not sure why. Is there a way to return only the head (i.e. first 5 rows) with a loop?
Oops. I initialized the size incorrectly. And from there on it was simply: for(int i = 0; i < 5;i++){ for(int o = 0; o < 3; o++){ System.out.print(levelList.get(o)[i] + ", "); } System.out.println(); }

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.