Re: how to return Comparator values




"Thufir" <hawat.thufir@xxxxxxxxx> wrote in message
news:a16210b1-83f6-4088-b331-1f2612779042@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Just trying to learn how Comparator works.

Looking at:

<snip code/>

I like the line:

Collections.sort(e, SENIORITY_ORDER); //this will sort the collection
"e" by //SENIORITY_ORDER?

However, how or where is SENIORITY_ORDER ranking defined? I guess
it's in

return e2.hireDate().compareTo(e1.hireDate()); //neg, zero, pos

which returns either a negative, positive or zero. We're only
interested in negative results?

Of course, you should read the Javadocs for the Comparator interface, but in
a nutshell, the compare (a, b) function returns the relative order of the
two items.
If a < b, it should return -1
if a > b it should return +1
if a.equals(b) it should 0

It's up to you to define what "<" means by looking at your own data. The
compareTo function of Comparables (Strings, Numbers, etc all implement
Comparable) uses the same definition, but you can prioritize the data as you
see fit to determine how you want the order.

Matthew Humphrey http://www.iviz.com/


.



Relevant Pages

  • Re: String.equals()
    ... if you know that what you'll submit it to will only care ... whether it gives zero or non-zero? ... So just make a Comparator that returns 0 or 1, and put a note in the ...
    (comp.lang.java.programmer)
  • noop comparator for qsort
    ... The fourth argument is a comparator that returns `an integer less than, ... or greater than zero' depending on the ordering of its ... If I don't care about the order and simply want qsort() to run as ...
    (comp.lang.c)
  • Re: Synchronous rectifier
    ... I have said that the comparator is a zero-crossing detector. ... used to compare Vds with zero, but in fact there are some offset ... a negative several milivolts will cause oscillation under ...
    (sci.electronics.design)
  • Re: noop comparator for qsort
    ... > The fourth argument is a comparator that returns `an integer less than, ... or greater than zero' depending on the ordering of its ... Kevin Bagust. ...
    (comp.lang.c)