Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

comparator example needed

 
Mick Smith
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mick Smith
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would recommend using 2 comparators:
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
S Majumder
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic