File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to perform multi-level sorting in java?

 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • 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.


 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic