I need some generic comparator which would accept instances of List or Set as an argument, and order direction argument (ASC, DESC), and then return the sorted collection. I can not seem to find on internet this example, and am in a horrible rush. I know I dont ask question in appropriate way, since I dont have any code to begin with but am in a horrible rush. Collections will contain objects which implement comparable and dates.
Any examples, implementations very much appreciated.
Thank you.
The Collections class has a
reverseOrdermethod which returns a comparator for a generic typeTwhich should satisfy your requirement for the DESC comparator. If you are passing yourCollectionto theCollections.sort()method, it automatically uses the ASC sort.Also, “sorting” doesn’t mean a lot when it comes to “sets” which maintain “unique” elements in an unordered fashion (I mean you can use
TreeSetfor sorted sets, but that’s a different story). A simple workaround would be to make aListout of theSetand pass it toCollections.sort.