Tylones
Tylones

Reputation: 127

Sorting an ArrayList of arrays in java

I'd like to know if you know a way to sort an ArrayList of arrays in Java. I have a function that gives a score between 0 and 1 to a specific array. And I'd like to sort the ArrayList so that arrays having the highest score come first.

public double evaluate(int[] toEvaluate) {
    double result = 0.0;
    for (int i = 0; i < toEvaluate.length; i++) {
        result += table[i][casesMap.get(toEvaluate[i])];
    }
    return result / toEvaluate.length;
}

Any ideas ?

Upvotes: 0

Views: 1876

Answers (4)

syntagma
syntagma

Reputation: 24304

You should use Collections.sort() together with a custom Comparator:

List<Integer[]> arrays = new ArrayList<>();

arrays.add(new Integer[]{1, 2});
arrays.add(new Integer[]{3, 4});

Collections.sort(arrays, new Comparator<Integer[]>() {
    public int compare(Integer[] a, Integer[] b) {
        return 1; // FIX this according to your needs
    }
});

compare() above is just a stub, you should implement it according to the documentation and it could be replaced with a lambda expression. In the code above that would be: Collections.sort(arrays, (a, b) -> 1).

Upvotes: 4

Anton Balaniuc
Anton Balaniuc

Reputation: 11739

You might want to use stream api for that. So let's say we have the scoring function (I simplified it for the sake of example).

public static double evaluate(int[] arr){
     return Arrays.stream(arr).sum() / arr.length;
}

Now we can use it with Comparator.comparing method:

List<int[]> list = Arrays.asList(new int[]{4, 5}, 
                          new int[]{2, 3}, new int[]{0, 1});
List<int[]> sorted = list.stream().
        sorted(Comparator.comparing(Main::evaluate)).
        collect(Collectors.toList());

sorted.forEach(x -> System.out.println(Arrays.toString(x)));

The idea behind the code is quite simple, you provide a comparator which defines how to sort int[] arrays. I hope this helps.

Upvotes: 0

Amit
Amit

Reputation: 1

You can either use comparator to sort list in descending order or you can use Collections sort method and then use reverse method to make it descending order, something like this :

    List<Integer> numberList =new ArrayList<Integer>();
    numberList.add(3);
    numberList.add(1);
    numberList.add(2);

    //before sort
    for (Integer integer : numberList) {
        System.out.println(integer);
    }

    //sorting
    Collections.sort(numberList);
    Collections.reverse(numberList);

    //after sort
    for (Integer integer : numberList) {
        System.out.println(integer);
    }

Upvotes: 0

gati sahu
gati sahu

Reputation: 2626

You have to write a Comparator and compare method override where you can use your function to calculate comp value

  @Override
                public int compare(Integer[] o1, Integer[] o2) {
                 int o1Number=ratingFunction(o1) ;
                 int o2Number=ratingFunction(o2) ;  
                 int cmp=o1Number.compareTo(o2Number);
                    return cmp;
                }

Upvotes: 0

Related Questions