• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sorting list of Bean

 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 218
VI Editor Ruby Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Muhammad Ali",

Please read your private messages regarding an important announcement.

Thank you,

Rob
 
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That really helped. Thanks guys
 
There's a hole in the bucket, dear Liza, dear Liza, a hole in the bucket, dear liza, a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic