简体   繁体   English

将升序更改为降序

[英]change an ascending sort to descending

I have the following code to sort a list but I need to make it a descending sort, 我有以下代码对列表进行排序,但是我需要将其设为降序排序,

List list = new LinkedList(thismap.entrySet());
Collections.sort(list, new Comparator() {
    public int compare(Object o1, Object o2) {
        return ((Comparable) ((Map.Entry) (o2)).getValue())
                .compareTo(((Map.Entry) (o1)).getValue());
    }
});

Map output = new LinkedHashMap();
for (Iterator it = list.iterator(); it.hasNext();) {
    Map.Entry entry = (Map.Entry) it.next();
    output.put(entry.getKey(), entry.getValue());
}

A common, general purpose technique is to wrap a Comparator in a reverse Comparator by simply swapping the arguments. 一种通用的通用技术是通过简单地交换参数来将比较器包装在反向比较器中。

class ReverseComparator<T> implements Comparator<T> {

    private final Comparator target;

    public ReverseComparator(Comparator<T> target) {
        super();
        this.target = target;
    }

    public int compare(T first, T second) {
        return target.compare(second, first);
    } 
}

To use it with our example: 在我们的示例中使用它:

Comparator original = new Comparator() {
    public int compare(Object o1, Object o2) {
        return ((Comparable) ((Map.Entry) (o2)).getValue())
            .compareTo(((Map.Entry) (o1)).getValue());
    }
};

Collections.sort(list, new ReverseComparator(original));

The simple general answer is to use java.util.Collections.reverseOrder(Comparator) . 简单的一般答案是使用java.util.Collections.reverseOrder(Comparator)

Comparator myComparator = new Comparator() {
    public int compare(Object o1, Object o2) {
    return ((Comparable) ((Map.Entry) (o2)).getValue())
        .compareTo(((Map.Entry) (o1)).getValue());
    }
}
// ... or whatever.

Comparator myReverseComparator = Collections.reverseOrder(myComparator);

Alternatively, a specific solution would be to flip the parameters in the compare method: 或者,一种特定的解决方案是在compare方法中翻转参数:

Comparator myReverseComparator = new Comparator() {
    public int compare(Object o2, Object o1) {  // <== NOTE - params reversed!!
    return ((Comparable) ((Map.Entry) (o2)).getValue())
        .compareTo(((Map.Entry) (o1)).getValue());
    }
}

Note that multiplying by -1 is an incorrect solution because of the Integer.MIN_VALUE edge case. 请注意,由于边缘为Integer.MIN_VALUE ,因此乘以-1不正确的解决方案。 Integer.MIN_VALUE * -1 is ... Integer.MIN_VALUE Integer.MIN_VALUE * -1是... Integer.MIN_VALUE

It's all about changing the content of the method below: 全部与更改以下方法的内容有关:

public int compare(Object o1, Object o2) 
{
    return ((Comparable) ((Map.Entry) (o2)).getValue())
           .compareTo(((Map.Entry) (o1)).getValue());
}

Return a different result than the value of the statement below: 返回与以下语句的值不同的结果:

((Comparable)((Map.Entry)(o2)).getValue()).compareTo(((Map.Entry)(o1)).getValue());

Let's say the statement above is assigned to x. 假设上面的语句分配给x。 Then you should return 1 if x < 0, return -1 if x > 0 and return 0 if x == 0, just inside the compare() method. 然后,如果x <0,则应返回1;如果x> 0,则应返回-1;如果x == 0,则应返回0,仅在compare()方法内部。

So your method could look like this: 因此,您的方法可能如下所示:

public int compare(Object o1, Object o2) 
{
   int x = ((Comparable)((Map.Entry)(o2)).getValue())
             .compareTo(((Map.Entry)(o1)).getValue());
   if(x > 0)
     return -1;
   else if (x < 0)
     return 1;

   return 0;
}

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

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