Sort List in reverse in order

后端 未结 8 701
别那么骄傲
别那么骄傲 2020-11-29 09:03

I have List list1 in direct order. List list = Ordering.natural().sortedCopy(asu2);

How to change order. And I don\'t know how to rewrite

相关标签:
8条回答
  • 2020-11-29 09:24

    you can reverse any type by just putting "-" or negative sign infront of your return.

    Collections.sort(listed, new Comparator<Object>() {
    
        @Override
        public int compare(Object o1, Object o2) {
    
            return -o1.getLeft().compareTo(o2.getLeft());
    
        }
    });
    
    0 讨论(0)
  • 2020-11-29 09:30

    Collections.reverse(List) reverses the given list in place. You can also use Guava's Lists.reverse(List) to create a view backed by the given list that is in reverse order without copying it or modifying the original list.

    And if you just want to create a list that is sorted in the reverse of the natural order, see @assylias's answer about Ordering.natural().reverse().

    0 讨论(0)
  • 2020-11-29 09:31

    list.sort(Comparator.reverseOrder());

    0 讨论(0)
  • 2020-11-29 09:33

    To reverse the order of items in List you can use Collections.reverse(List<?> list)

    0 讨论(0)
  • 2020-11-29 09:34

    There is a method reverseOrder in the Collections class which returns a Comparator.

    You can use it like Collections.sort(list, Collections.reverseOrder());

    0 讨论(0)
  • 2020-11-29 09:35

    If you want to sort the list in reverse natural order, guava's Ordering has a reverse method:

    List<String> list = Ordering.natural().reverse().sortedCopy(asu2);
    
    0 讨论(0)
提交回复
热议问题