有没有一种方法可以使用方法引用进行降序排序?
Is there a way to sort descending with a method reference?
在 Java 8 中,我可以使用 lambda 或方法参考进行排序:
List<String> str = Arrays.asList("a","b","A","B");
//Lambda sort ascending
str.sort(s1, s2) -> s1.compareToIgnoreCase(s2);
//Equivalent sort ascending with method reference
str.sort(String::compareToIgnoreCase);
//Lambda sort descending
str.sort(s1, s2) -> s2.compareToIgnoreCase(s1);
//Equivalent sort descending with method reference
???????? How to do it????
有人知道吗?
您可以使用Comparator#reversed
str.sort(((Comparator<String>)String::compareToIgnoreCase).reversed());
或两步
Comparator<String> comp = String::compareToIgnoreCase;
str.sort(comp.reversed());
在 Java 8 中,我可以使用 lambda 或方法参考进行排序:
List<String> str = Arrays.asList("a","b","A","B");
//Lambda sort ascending
str.sort(s1, s2) -> s1.compareToIgnoreCase(s2);
//Equivalent sort ascending with method reference
str.sort(String::compareToIgnoreCase);
//Lambda sort descending
str.sort(s1, s2) -> s2.compareToIgnoreCase(s1);
//Equivalent sort descending with method reference
???????? How to do it????
有人知道吗?
您可以使用Comparator#reversed
str.sort(((Comparator<String>)String::compareToIgnoreCase).reversed());
或两步
Comparator<String> comp = String::compareToIgnoreCase;
str.sort(comp.reversed());