• 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

How to perform multi-level sorting in java?

 
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a requirement where when the user sorts by particular property the sorting has to be done on other properties too.

Like a bean having following properties
name
age
sex
address
company

then If I am sorting by name the sorting should be
1. sort first by name followed by age followed by sex then address and then company..

any idea to do this.. in java?
is someone aware of some open source API to do it?

Please help.

Thanks in advance.


 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can do this wit ha java.util.Comparator. Have a read of the JavaDocs, it should make sense.
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul for the reply.
Yes we can do it using Comparator but in that case there will be collection.size * 5 iterations as I have to do 5 level deep sorting.

The size of the collection will be at least 1500 and that means at every click i have to iterate 7500 times. Being a web application I have to also consider the time taken to do it.

Do we have some better approach?

Thanks once again.
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Yes we can do it using Comparator but in that case there will be collection.size * 5 iterations as I have to do 5 level deep sorting


Not sure I follow. Where do the other 4 iterations come from?
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm...

Like I sorted it using name. Now maintain that sorted order I have to sort on other properties.

Hope this states my problem clearly.
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So why not sort on all five properties in the one compare method?
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats good for the first time. But We have data-table which are shown to the user. It has 10 columns. Now whenever the user clicks on any header we have to perform 5 level deep sorting. So its not only one time sorting but the list will be sorted on each click of the user.
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what you are actually doing is sorting on single properties multiple times? You can still do this with a Comparator, but you'll need to pass the properties you are sorting on to it as well. Because the sort is initiated by the user you will have to itertate back through the collection every time they click a property.
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul. I was searching for something like ComparatorChain.
http://commons.apache.org/collections/api-2.1.1/org/apache/commons/collections/comparators/ComparatorChain.html

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic