aspose file tools*
The moose likes Java in General and the fly likes Sorting ArrayList of MemberBean objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sorting ArrayList of MemberBean objects" Watch "Sorting ArrayList of MemberBean objects" New topic
Author

Sorting ArrayList of MemberBean objects

Rahul Pandey
Greenhorn

Joined: Feb 21, 2007
Posts: 12
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!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
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

Joined: Feb 21, 2007
Posts: 12
Thanks CR!
Rahul Pandey
Greenhorn

Joined: Feb 21, 2007
Posts: 12
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!!
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 852
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:



http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 852
Here is a reflective version I found on google, but with getName hard coded.

Rahul Pandey
Greenhorn

Joined: Feb 21, 2007
Posts: 12
Ahh! Just the perfect generic solution I was looking for!!

Thanks a ton, Steve and CR!
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 852
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

Joined: Jun 26, 2002
Posts: 852
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

Joined: Jun 26, 2002
Posts: 852
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

Joined: Feb 21, 2007
Posts: 12
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!
jay lai
Ranch Hand

Joined: Apr 04, 2002
Posts: 180
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

Joined: Jun 26, 2002
Posts: 852
A solution like this does exactly what you are requesting.

jay lai
Ranch Hand

Joined: Apr 04, 2002
Posts: 180
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting ArrayList of MemberBean objects
 
Similar Threads
Sorting
Sorting collection of objects
object sorting in a Vector
Sort Arraylist by passing runtime parameter
Sort Objects by Comparator