Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何根据另一个ArrayList对ArrayList进行排序?_Java_Sorting_Arraylist - Fatal编程技术网

Java 如何根据另一个ArrayList对ArrayList进行排序?

Java 如何根据另一个ArrayList对ArrayList进行排序?,java,sorting,arraylist,Java,Sorting,Arraylist,我有两个ArrayList: 任何想法?考虑到两个列表之间没有关系定义,我认为你应该考虑在一个类中包装这两个值: class Foo { private String name; private double rating; public Foo(String name, double rating){ this.name = name; this.rating = rating; } // getters & setter

我有两个
ArrayList


任何想法?

考虑到两个列表之间没有关系定义,我认为你应该考虑在一个类中包装这两个值:

class Foo {

   private String name;
   private double rating;

   public Foo(String name, double rating){
       this.name = name;
       this.rating = rating;
   }

   // getters & setters
}
然后,您可以使用
列表
,并根据评级值对列表进行排序


在Java 8中,这就像调用
sort()
方法一样简单,该方法现在在
List
中提供,并传入一个lambda表达式。

让我们使用
映射来解决这个问题。我们将首先定义一个比较器来按值降序排序,如下所示:

class NumericComparator extends Comparator {
    Map map;
    public NumericComparator(Map map) {
        this.map = map;
    }

    public int compare(Object o1, Object o2) {
        return ((Integer) map.get(o2)).compareTo((Integer) map.get(o1));
    }
}
我们将名称定义为键,评级定义为值。像这样:

Map<String, Double> carMap = new HashMap<>();
//we can populate the map like so:
carMap.put("John", 10d); //works nicely in loops

首先,请编程到
列表
界面。接下来,我假设您可以使用apache库(或者您可以实现自己的元组类;或者使用其他类似的类)。无论如何,您需要修改您的
比较器
,以便在右侧操作(我个人喜欢使用左侧来断开连接)。最后,如果要使用
label1
label2
,则需要将值复制回来(排序后)。大概

List<JLabel> label1 = new ArrayList<>();
List<JLabel> label2 = new ArrayList<>();
// ...
List<Pair<JLabel, JLabel>> pairs = new ArrayList<>();
int len = label1.size();
if (len == label2.size()) {
    for (int i = 0; i < len; i++) {
        pairs.add(Pair.of(label1.get(i), label2.get(i)));
    }
    Collections.sort(pairs, new Comparator<Pair<JLabel, JLabel>>() {
        @Override
        public int compare(Pair<JLabel, JLabel> o1,
                Pair<JLabel, JLabel> o2) {
            double a = Double.parseDouble(o1.getRight().getText());
            double b = Double.parseDouble(o2.getRight().getText());
            int ret = Double.compare(a, b);
            if (ret != 0) {
                return ret;
            } // if ret is 0, it's a tie.
            return o1.getLeft().getText()
                    .compareTo(o2.getLeft().getText());
        }
    });
    // ... only if you need to use label1 and label2 after the sort.
    for (int i = 0; i < len; i++) {
        label1.set(i, pairs.get(i).getLeft());
        label2.set(i, pairs.get(i).getRight());
    }
}
List label1=new ArrayList();
List label2=新的ArrayList();
// ...
列表对=新的ArrayList();
int len=label1.size();
if(len==label2.size()){
对于(int i=0;i
您能给出一个输入输出示例吗?除了这个答案,我还要补充一点,您可以使用方法引用进行排序<代码>列表.sort(Comparator.comparingDouble(Foo::getRating))
Map<String, Double> carMap = new HashMap<>();
//we can populate the map like so:
carMap.put("John", 10d); //works nicely in loops
NumericComparator nc = new NumericComparator(carMap);
Map<String, Double> sortedMap = new TreeMap(nc);
sortedMap.putAll(carMap);
List<JLabel> label1 = new ArrayList<>();
List<JLabel> label2 = new ArrayList<>();
// ...
List<Pair<JLabel, JLabel>> pairs = new ArrayList<>();
int len = label1.size();
if (len == label2.size()) {
    for (int i = 0; i < len; i++) {
        pairs.add(Pair.of(label1.get(i), label2.get(i)));
    }
    Collections.sort(pairs, new Comparator<Pair<JLabel, JLabel>>() {
        @Override
        public int compare(Pair<JLabel, JLabel> o1,
                Pair<JLabel, JLabel> o2) {
            double a = Double.parseDouble(o1.getRight().getText());
            double b = Double.parseDouble(o2.getRight().getText());
            int ret = Double.compare(a, b);
            if (ret != 0) {
                return ret;
            } // if ret is 0, it's a tie.
            return o1.getLeft().getText()
                    .compareTo(o2.getLeft().getText());
        }
    });
    // ... only if you need to use label1 and label2 after the sort.
    for (int i = 0; i < len; i++) {
        label1.set(i, pairs.get(i).getLeft());
        label2.set(i, pairs.get(i).getRight());
    }
}