It's not a secret anymore!*
The moose likes Java in General and the fly likes comparator example needed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "comparator example needed" Watch "comparator example needed" New topic
Author

comparator example needed

Mick Smith
Greenhorn

Joined: Dec 10, 2009
Posts: 14
In my application I will have tables that require sorting by the column heading.
As such I will need to implement comparators to do this.

Has anyone got any suggestions on a generic comparator class that I can use so that I can pass in 2 objects and also the field(column header) that I want to compare?

Do I have to create a comparator for every column heading? I certainly hope not.

any ideas or examples would be really appreciated.

Thanks in advance.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Have you looked at the Comparator javadoc?
Have you search for something like "java comparator tutorial"?
Because then you would have found this java.sun.com tutorial.

But to answer one of your questions. No you don't have to create a comparator for every column. Just for each object that you want to compare.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Mick Smith
Greenhorn

Joined: Dec 10, 2009
Posts: 14
Yes I have looked at the java sun tutorial. Maybe I didnt make myself quite clear in my orignal post.

In the examples I have seen so far when comparing objects, it seems that each comparator can only compare 1 field of the objects at a time.
I would like to have 1 comparator for each object that can compare any of the all of the fields of that object.
As an example from this website: comparator example, they have 2 comparators 1 for age and 1 for name. can I have 1 comparator to handle both??



Has 1 comparator soley to compare name:
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

I would recommend using 2 comparators:
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3419
    
  12
Mick Smith wrote:Yes I have looked at the java sun tutorial. Maybe I didnt make myself quite clear in my orignal post.

In the examples I have seen so far when comparing objects, it seems that each comparator can only compare 1 field of the objects at a time.


You may need to read it again. The first piece of example code does a comparison based on two fields (first and last name).


Joanne
S Majumder
Ranch Hand

Joined: Jun 03, 2009
Posts: 244
Hello Mick Smith,
I think I can help you out from this problem.Just check each and every class, run the application it will give you the desire result.

Please fill free if you have any doughts/confusion.


Emp2.java its is a simple POJO , you just replace this class as your requirment:




SortUtil.java the client code :here I am making the list of data & sorting by my choice.Here I am sorting by ID.



GenericSort.java , here is the main magic, the actual business logic is in this class:



enjoy

Thanks and regards,
S
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: comparator example needed
 
Similar Threads
Sorting a ResultSet
Centering a column heading in a datatable
How to determine size of TableColumn text?
adding param value dynamically html:link forward tag?
How to wrap text in csv in Jasper report