aspose file tools*
The moose likes Java in General and the fly likes How to perform multi-level sorting in java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to perform multi-level sorting in java?" Watch "How to perform multi-level sorting in java?" New topic
Author

How to perform multi-level sorting in java?

Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

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.



My Blog SCJP 5 SCWCD 5
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

You can do this wit ha java.util.Comparator. Have a read of the JavaDocs, it should make sense.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

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

Joined: Apr 14, 2004
Posts: 10336


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

Joined: Aug 18, 2008
Posts: 598

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

Joined: Apr 14, 2004
Posts: 10336

So why not sort on all five properties in the one compare method?
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

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

Joined: Apr 14, 2004
Posts: 10336

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

Joined: Aug 18, 2008
Posts: 598

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
 
subject: How to perform multi-level sorting in java?