• 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

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Adam Vinueza
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic