Question

I have an arraylist which holds arrays of double values and I want to sort the rows for example by the third value in the field.

Is there a smart way to do it? (ascending/descending)

declaration:

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

example:

  • 10.0, 5.0, 200.3
  • 9.1, 1.4, 3.3
  • 1.1, 7.6, 3.4

sorts by the second column (asc)

  • 9.1, 1.4, 3.3
  • 10.0, 5.0, 200.3
  • 1.1, 7.6, 3.4

EDIT!!

later iam gonna try this but imo I have no time to test it! mby someone could verify this

    if (sortDir == SortDirection.ASCENDING) {
        Collections.sort(tabelle,new Comparator<double[]>() {
            public int compare(double[] array1, double[] array2) {
                return Double.compare(array1[colIds.indexOf(aColId)], array2[colIds.indexOf(aColId)]);
            }
        });
    }
    if (sortDir == SortDirection.DESCENDING) {
        Collections.sort(tabelle,new Comparator<double[]>() {
            public int compare(double[] array1, double[] array2) {
                return Double.compare(array1[colIds.indexOf(aColId)], array2[colIds.indexOf(aColId)]);
            }
        });
    Collections.reverse(tabelle);
    }

wow soo many comments :D thanks

Was it helpful?

Solution

In Java 8:

public static void main(final String[] args) throws IOException {
    final List<double[]> row = new ArrayList<double[]>();

    row.add(new double[]{10.0, 5.0, 200.3});
    row.add(new double[]{9.1, 1.4, 3.3});
    row.add(new double[]{9.1, 1.4, 3.3});

    final List<double[]> sorted = row.stream().
            sorted((l, r) -> Double.compare(l[2], r[2])).
            collect(Collectors.toList());
}

Or, if you want to sort in place:

row.sort((l, r) -> Double.compare(l[2], r[2]));

OTHER TIPS

Collections.sort() can take a Comparator.

See the API.

using Collections.sort()

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

row.add(new double[] { 10.0, 5.0, 200.3 });
row.add(new double[] { 9.1, 1.4, 3.3 });
row.add(new double[] { 1.1, 7.6, 3.4 });

Collections.sort(row, new Comparator<double[]>() {

    @Override
    public int compare(double[] o1, double[] o2) {
        return Double.compare(o1[1],o2[1]);
    }
});

hi I have made a function which takes list of arrays, and the Position of column which is to be compare and short the list...

public void tobeShorted(List<double[]> l, int c){  //short the list by column wise comparision
        for(int i=0; i<l.size()-1; i++){ //using Bubble short Technique 
            double[] current1=l.get(i); //get the current array
            for(int j=i+1; j<l.size(); j++){
                double[] current2=l.get(j); //get the next array
                if(current1[c-1]<current2[c-1]){ //compare the element of the given POSITION(not index) of arrays
                    l.remove(j);l.add(j, l.get(i)); //remove and add rows
                    l.remove(i);l.add(i, current2); //remove and add rows
                }
            }
        }
    }

I have check it and it is working and it short the list in Descending Order....I wish it may help you...!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top