• 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 ArrayList of MemberBean objects

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have an ArrayList which contains MemberBean objects. The MemberBean is displayed in the JSP as a table with four fields - First Name, Last Name, Member ID (integer) and User Name.

I need to sort this based on user selected field and in either ascending or descending order.

I know I can use the Comparator interface with the Collections.sort(List,Comparator) but somehow I am not getting around to making a generic Comparator which will sort based on any input field.

Can anyone help me with this?

Thanks in advance!
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.
You will have to go through the API and find Comparator and set up four classes which implement Comparator. Set up each of the methods as a compareTo() method. You can simply usebecause Strings already implement the Comparable interface.
For ID numbers you can get away with something likeNote that you will have to fulfil the requirements in the API specifications for Comparator.

To sort backwards, try the reverse method of Collections.

CR
 
Rahul Pandey
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks CR!
 
Rahul Pandey
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is how my final implementation looks like..

The function sortMemberList is placed in a common Util class and called from wherever the sorting needs to be done.
--------------------------
----------------------------

To avoid making four different classes, I incorporated all the custom comparator classes into the Util class itself as inner-classes. It really simplifies your code structure.

----------------------------
-----------------------------

And finally, from my implementation class, this is how I called the sort functionality...

-----------------------------



-----------------------------

That's it! Its really quite simple when you think about it!!
 
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another interesting more generic solution to this problem would be to implement a Comparator using Reflection. I have used psuedo code to do this below.



Then you could use the code in the following manner.


Also a cool open source project is josql that allows you to query collections in a declaritive way (something like 'select * from collection order by getLastName() desc, getFirstName() asc'): http://josql.sourceforge.net/

My FormattedDataSet API has the ability to use a sql like syntax on Object[][]. You can query by name or position. An example of using ArraySQL to query by position follows:

 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a reflective version I found on google, but with getName hard coded.

 
Rahul Pandey
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahh! Just the perfect generic solution I was looking for!!

Thanks a ton, Steve and CR!
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Post the code that you come up with. I would like to see and comment on the end result. I should also say your original solution was good too.
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I cleaned up the code from my postings above. I still didn't compile it however. If you test it or modify it and it works, let me know. That code could be handy...


[ February 22, 2007: Message edited by: steve souza ]
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you may also familiarize yourself with these useful comparators:

http://jakarta.apache.org/commons/collections/api-release/org/apache/commons/collections/comparators/package-summary.html
 
Rahul Pandey
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
steve, here's my final version...this worked beautifully...though the code posted by you above also works just as well:

first the reflection comparator class..



then the sortlist function...


finally, the function call to sort the list by First Name...



thanks for all the help...the link was useful too!
 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rahul:

I need to sort 2 columns out of 4 columns retrieve from database.


To look into your code, try to understand what are "FirstName", "LastName", where exactly this reference to? Thanks


 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A solution like this does exactly what you are requesting.

 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you elaborate on this? Do I have to re-write the method compare? I did what you recommended, but it is not throwing exception.

Many thanks in advance
 
It's just a flesh wound! Or a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic