Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Comparator.comparing() method.  RSS feed

 
Renato Perini
Greenhorn
Posts: 18
Eclipse IDE Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I have some difficulty understanding how this method works.
The Java SE 8 API Reference says:

Accepts a function that extracts a Comparable sort key from a type T, and returns a Comparator<T> that compares by that sort key.


I see many examples that use this method combined with the sort() method. For example:



The sentence that bog me is that "sort key". What is it? From the documentation, I read that the comparing() method returns a Comparator.
Using method references, the example I have posted uses Apple::getWeight
Does this mean that comparing(Apple::getWeight) returns a Comparator<Apple> similar to the following?


Am I correct? Can someone clarify better what is a "sort key"? Thank you.
 
Campbell Ritchie
Marshal
Posts: 55680
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that example from the Urma Fusco and Mycroft book? Always tell us where such examples come from.

Try
System.out.println(Comparator.comparing(Apple::getWeight));
and
applesList.sort(Comparator.comparing(Apple::getWeight));
Remember getWeight does not return an int, but an Integer.
 
Campbell Ritchie
Marshal
Posts: 55680
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few minuste ago, I wrote: . . . Remember getWeight does not return an int, but an Integer.
I suspect it may not matter whether you have an Integer or an int, because it will be boxed to an Integer.
 
Renato Perini
Greenhorn
Posts: 18
Eclipse IDE Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Is that example from the Urma Fusco and Mycroft book? Always tell us where such examples come from.

Try
System.out.println(Comparator.comparing(Apple::getWeight));
and
applesList.sort(Comparator.comparing(Apple::getWeight));
Remember getWeight does not return an int, but an Integer.


Oh yes, sorry. Java 8 in Action by Urma-Fusco-Mycroft is what I'm reading right now.
The println() prints:
java.util.Comparator$$Lambda$2/1555009629@8efb846

and the sorting works correctly.
 
Campbell Ritchie
Marshal
Posts: 55680
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought it lookedfamiliar.
The answer to whether that method returns a Comparator is in the printout; you have got the class name before the @ sign (obviously that class doesn't override Object#toString), and you can check the details in the documentation.
 
Renato Perini
Greenhorn
Posts: 18
Eclipse IDE Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I thought it lookedfamiliar.
The answer to whether that method returns a Comparator is in the printout; you have got the class name before the @ sign (obviously that class doesn't override Object#toString), and you can check the details in the documentation.


I already know that the Comparator.comparing() method returns a Comparator, no need to redirect me on the documentation when the documentation is the first thing I have mentioned.
My question is different. I was asking if the returned Comparator returned is similar to that I have provided in OP.
Also, I was asking for the "sort key".
 
Rob Spoor
Sheriff
Posts: 21087
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Renato Perini wrote:My question is different. I was asking if the returned Comparator returned is similar to that I have provided in OP.

Yes it is. Both compare using the result of the getWeight() method call.

Also, I was asking for the "sort key".

As documented, the function extracts a Comparable sort key. This sort key is the return value of the function. In this example, the sort key is the return value of the getWeight() method (a.k.a. the weight).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!