简体   繁体   English

在Java中对数组的ArrayList进行排序

[英]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. 我想知道您是否知道在Java中对数组的ArrayList进行排序的方法。 I have a function that gives a score between 0 and 1 to a specific array. 我有一个函数,可将特定数组的得分在0到1之间。 And I'd like to sort the ArrayList so that arrays having the highest score come first. 而且我想对ArrayList进行排序,以使得分最高的数组排在第一位。

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 ? 有任何想法吗 ?

You should use Collections.sort() together with a custom Comparator : 您应该将Collections.sort()与自定义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. 上面的compare()只是一个存根,您应该根据文档实现它,并且可以将其替换为lambda表达式。 In the code above that would be: Collections.sort(arrays, (a, b) -> 1) . 在上面的代码中将是: Collections.sort(arrays, (a, b) -> 1)

You have to write a Comparator and compare method override where you can use your function to calculate comp value 您必须编写一个Comparator并比较方法重写,您可以在其中使用函数来计算comp值

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

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 : 您可以使用比较器按降序对列表进行排序,也可以使用Collections的排序方法,然后使用反向方法使其按降序排列,如下所示:

    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);
    }

You might want to use stream api for that. 您可能要为此使用stream api。 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: 现在,我们可以将其与Comparator.comparing方法一起使用:

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. 代码背后的想法很简单,您提供一个比较器,该比较器定义如何对int[]数组进行排序。 I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM