Search results
Comparable allows you to specify how objects that you are implementing get compared. Obviously, if you don't have control over a class (or you want to provide multiple ways to compare objects that you do have control over) then use Comparator. Otherwise you can use Comparable. answered Nov 5, 2010 at 17:52.
Feb 15, 2010 · Use Comparable: if the object is in your control. if the comparing behaviour is the main comparing behaviour. Use Comparator : if the object is outside your control and you cannot make them implement Comparable. when you want comparing behaviour different from the default (which is specified by Comparable) behaviour.
Comparator<Person> comparator = Comparator.comparing(person -> person.name); comparator = comparator.thenComparing(Comparator.comparing(person -> person.age)); Check out the link above for a neater way and an explanation about how Java's type inference makes it a bit more clunky to define compared to LINQ.
If you are using Java 8 then it's better to use below code like this: Comparator<People> comparator = Comparator.comparing(People::getName); And then simply use: Collections.sort(list, comparator); If you are using Java 7 or below then you can use a comparator for customized sorting order by implementing compare method.
Sep 22, 2010 · From the reference docs of Comparable.compareTo (T): Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. The implementor must ensure sgn (x.compareTo (y)) == -sgn (y.compareTo (x)) for all x and y.
Jan 7, 2009 · compareTo() is from the Comparable interface. compare() is from the Comparator interface. Both methods do the same thing, but each interface is used in a slightly different context. The Comparable interface is used to impose a natural ordering on the objects of the implementing class.
The Comparator interface is used to establish an ordering for the elements being compared. Having fields that use double is irrelevant to this ordering. Your code is fine.
Oct 7, 2015 · I have an ArrayList and want sort it in descending order. I use for it java.util.stream.Stream.sorted(Comparator) method. Here is a description according Java API: Returns a stream consisting of the elements of this stream, sorted according to the provided Comparator. this methods return me a sort with ascending order.
The java.util.Collections class has a sort method that takes a list and a custom Comparator. You can define your own Comparator to sort your Person object however you like. You can define your own Comparator to sort your Person object however you like.
Sep 17, 2009 · A comparable object is capable of comparing itself with another object. The class itself must implements the java.lang.Comparable interface in order to be able to compare its instances. Comparator. A comparator object is capable of comparing two different objects. The class is not comparing its instances, but some other class’s instances.