Java Comparator using .reverseOrder() but with an inner class

后端 未结 4 1710
借酒劲吻你
借酒劲吻你 2021-02-05 03:42

I am creating a simple program to learn about the Java Comparator class. I have sorted an Arraylist into order but now I want to sort the list in descending order b

相关标签:
4条回答
  • 2021-02-05 03:58

    Let's take a simple example we have a class Person with two fields name age and we want to sort an existing collection of persons based on their age so let's assume that we have a class Person with a constructor and add the persons into the list and then sort them unsing the method sort of collection :

    Person bachiri = new Person (17,"bachiri");
    Person taoufiq = new Person (14,"Taoufiq");
    Person abderrahman = new Person (15,"abderrahman");
    List<Person> persons =  new ArrayList<>();
    

    and this this the impelemtation of Agecomparable :

    class AgeComparator implements Comparator<Person>{
    
    
        @Override
        public int compare(Person person1, Person person2) {
            return Integer.compare(person1.getAge(),person2.getAge());
        }
    
    
    }
    

    the trick is to multiple the return method with -1 so the final result will be reversed: class AgeComparator implements Comparator{

        @Override
        public int compare(Person person1, Person person2) {
            return -1 * Integer.compare(person1.getAge(),person2.getAge());
        }
    
    
    }
    

    so now we can get a reversed result :

    Collection.sort (Persons, new AgeComparator());
    
    0 讨论(0)
  • 2021-02-05 04:01

    If you need to use a Comparator which reverses the current order, just return a negative value in the compare method.

    public class ComparatorInverse implements Comparator<Object> {
       @Override
       public int compare(Object lhs, Object rhs) {
          return -1;
       }
    }
    
    0 讨论(0)
  • 2021-02-05 04:05

    One way to implement an reverse order comparator is to implement an Compartor-Delegate that invert the comparator result (by changing the order).

    public class ReverseOrder<T> implements Comparator<T> {
      private Comparator<T> delegate;
      public ReverseOrder(Comparator<T> delegate){
        this.delegate = delegate;
      }
    
      public int compare(T a, T b) {
        //reverse order of a and b!!!
        return this.delegate.compare(b,a);
      }
    }
    

    So the only thing you need to do is to use this delegate. For example:

      Comparator myComparator = new myComparator();
      List list = ...;
      List reverse = new ArrayList(list);
    
      //acceding
      Collections.sort(list, myComparator);
    
      //descending
      Collections.sort(list, new ReverseOrder(myComparator));
    
    0 讨论(0)
  • 2021-02-05 04:19
    ArtistCompare artistCompare = new ArtistCompare();
    Collections.sort(songList, Collections.reverseOrder(artistCompare));
    

    Edit July 2015

    As this answer still gets some attention, here a small update:

    With Java SE 8 it's becoming easier to create a reversed comparator:

    Comparator<Song> songRatingComparator = Comparator.comparing(Song::getRating);
    Collections.sort(songList, songRatingComparator.reversed());
    

    And you can, of course, also use the Streams framework:

    List<Song> sortedSongList = songList.stream()
    .sorted(Comparator.comparing(Song::getRating).reversed())
    .collect(Collectors.toList());
    
    0 讨论(0)
提交回复
热议问题