Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Java Help > how to return C...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 4 Topic 16027 of 16070
Post > Topic >>

how to return Comparator values

by Thufir <hawat.thufir@[EMAIL PROTECTED] > Apr 26, 2008 at 03:51 PM

Just trying to learn how Comparator works.

Looking at:

import java.util.*;
public class EmpSort {
    static final Comparator<Employee> SENIORITY_ORDER =
                                 new Comparator<Employee>() {
        public int compare(Employee e1, Employee e2) {
            return e2.hireDate().compareTo(e1.hireDate());
        }
    };

    // Employee database
    static final Collection<Employee> employees = ... ;

    public static void main(String[] args) {
        List<Employee>e = new ArrayList<Employee>(employees);
        Collections.sort(e, SENIORITY_ORDER);
        System.out.println(e);
    }
}

http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html

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?

I had a specific question, but sorta answered it while writing it...

anyhow.


-Thufir




 4 Posts in Topic:
how to return Comparator values
Thufir <hawat.thufir@[  2008-04-26 15:51:20 
Re: how to return Comparator values
"Matt Humphrey"  2008-04-26 19:49:56 
Re: how to return Comparator values
Lew <lew@[EMAIL PROTEC  2008-04-26 22:27:32 
Re: how to return Comparator values
Roedy Green <see_websi  2008-04-27 02:23:14 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sun May 11 21:28:09 CDT 2008.