aspose file tools*
The moose likes Java in General and the fly likes Sorting list 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 "Sorting list" Watch "Sorting list" New topic
Author

Sorting list

Aarti Pednekar
Greenhorn

Joined: Jan 06, 2005
Posts: 8
Hi,

I have a question on sorting of lists. I have 3 list objects objList1, objList2 and objList3 and each of these objects contain a Name field. I need to sort these lists each in ascending order of Name.
I am using a Bean Comparator for this prpose with the following logic,
BeanComparator comp = new BeanComparator(ATTRIBUTE_TO_BE_SORTED);
Collections.sort(objList1, comp);
Collections.sort(objList2, comp);
Collections.sort(objList3, comp);
This works fine but since Bean Comparator is used on the Webserver and I need to do this on my App server, I need another way to sort the lists.
Can you please make a suggestion.
Adam Vinueza
Ranch Hand

Joined: Apr 16, 2001
Posts: 76
Why not just write your own Comparator? It should take five minutes or so, if you know what to look for when comparing the objects.
Aarti Pednekar
Greenhorn

Joined: Jan 06, 2005
Posts: 8
Thanks for the reply but I do not want to use comparators to do this. Is there any other method apart from the normal sort and using a comparator
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

Have your bean implement java.lang.Comparable. Then you can call Collections.sort() without a Comparator.
What you should do is copy the jakarta commons bean utils jar file to your app server so you can use BeanComparator. Being consistent in coding is essential to writing a maintainable application, and reuse is one of the fundamental building blocks of object oriented programming.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Adam Vinueza
Ranch Hand

Joined: Apr 16, 2001
Posts: 76
That seems to be an odd restriction, but I suppose you could use the sort() method from Arrays, assuming the sorted objects are Strings. You'll have to do some jiggery-pokery--extract the names into an array, sort them, build new Lists and read the elements into them in sorted order using the sorted array as your guide--but it would work.

The reason comparators are the natural thing to use here is that the objects you're sorting probably have no natural ordering, or if they do, they have a natural ordering different from the ordering you want. I really don't see a way to do what you want in a natural way without using comparators.
Adam Vinueza
Ranch Hand

Joined: Apr 16, 2001
Posts: 76
I'd forgotten about implementing Comparable, which saves you the trouble of writing your own Comparator. But you have to implement the compareTo() method in either case. Joe's suggestion is strongly preferable to writing your own Comparator, assuming the objects to be sorted are always sorted in the same way.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting list