Collections.sort排序1值不正确

时间:2015-06-11 06:35:21

标签: java

我在调度算法的模拟中使用比较器实现Collections.sort,并且由于某种原因它错误地排序了1个值。

排序的预期输出应该是(中间截断):

PID=1, PEntry=1, PBurst=1
PID=2, PEntry=2, PBurst=1
.
.
.
PID=15, PEntry=15, PBurst=1
PID=16, PEntry=16, PBurst=1
PID=17, PEntry=17, PBurst=1

但它给了我这个(中间截断):

PID=1, PEntry=1, PBurst=1
PID=2, PEntry=2, PBurst=1
.
.
.
PID=15, PEntry=15, PBurst=1
PID=17, PEntry=17, PBurst=1
PID=16, PEntry=16, PBurst=1

比较

    private static Map<Integer, ArrayList<Integer>> sortByComparator(Map<Integer, ArrayList<Integer>> unsorted) {
            List<Map.Entry<Integer, ArrayList<Integer>>> linkedList = new LinkedList<Map.Entry<Integer, ArrayList<Integer>>>(unsorted.entrySet());
            Collections.sort(linkedList, new Comparator<Map.Entry<Integer, ArrayList<Integer>>>() {
                    public int compare(Map.Entry<Integer, ArrayList<Integer>> object1, Map.Entry<Integer, ArrayList<Integer>> object2) {
                            return (object2.getValue().get(0)).compareTo(object2.getValue().get(0));
                    }
            });
            Map<Integer, ArrayList<Integer>> sorted = new LinkedHashMap<Integer, ArrayList<Integer>>();
            for (Iterator<Map.Entry<Integer, ArrayList<Integer>>> it = linkedList.iterator(); it.hasNext();) {
                    Map.Entry<Integer, ArrayList<Integer>> entry = it.next();
                    sorted.put(entry.getKey(), entry.getValue());
            }
            return sorted;
    }

1 个答案:

答案 0 :(得分:0)

您正在将object2.getValue().get(0)与自身进行比较。应该是:

 return (object1.getValue().get(0)).compareTo(object2.getValue().get(0));