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

Sorting list of Bean

 
Em Aiy
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a bean class


This class is being retrieved to my by hibernate in form of list. I need to implementing sorting on it. I just read that i need to Comparable interface on it and then i can do the rest.

I have one question

What if i need to sort the List of this bean on different columns i.e at one place may be i need sorting at field1 and at some place may be i need at date and on the other place i'll be looking to sort the list on id.

What could be the best possible way of doing this?
 
Wirianto Djunaidi
Ranch Hand
Posts: 210
Ruby Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You use Comparator instead. You create a Comparator for each of your special case and use it with
 
Em Aiy
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For this I'd have to implement the sort method (which is not a problem). but that method can have only one SORTING rule i.e; either sort on field1, or field2 or date.

What i want is something which can help me sort the List on my require field.
 
Thomas Thevis
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originalliy posted by Muhammad Ali:
For this I'd have to implement the sort method.

Actually not. Wirianto Djunaidi is quite right. Have a look at the java.util.Comparator interface. You don't have to implement sort(), but to provide a Comparator which performs a comparison on pairs of your bean objects. For each column to sort, you can provide a different Comparator.

Regards,
Thomas
 
Rob Spoor
Sheriff
Pie
Posts: 20533
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Muhammad Ali",

Please read your private messages regarding an important announcement.

Thank you,

Rob
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
google with these keywords or similar ones and you should find how to do this...

java comparator multiple fields
 
Em Aiy
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That really helped. Thanks guys
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic