java 将升序更改为降序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12755736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
change an ascending sort to descending
提问by Eme Emertana
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());
}
回答by Brent Worden
A common, general purpose technique is to wrap a Comparator in a reverse Comparator by simply swapping the arguments.
一种常见的通用技术是通过简单地交换参数将 Comparator 包装在反向 Comparator 中。
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));
回答by Stephen C
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:
或者,一个特定的解决方案是翻转比较方法中的参数:
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 incorrectsolution because of the Integer.MIN_VALUE
edge case. Integer.MIN_VALUE * -1
is ... Integer.MIN_VALUE
请注意,由于边缘情况,乘以-1
是一个不正确的解决方案Integer.MIN_VALUE
。 Integer.MIN_VALUE * -1
是 ...Integer.MIN_VALUE
回答by Juvanis
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. 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。那么你应该在 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;
}