aspose file tools*
The moose likes Beginning Java and the fly likes Sorting of a Arraylist with multiple elements with sort order Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sorting of a Arraylist with multiple elements with sort order" Watch "Sorting of a Arraylist with multiple elements with sort order" New topic
Author

Sorting of a Arraylist with multiple elements with sort order

Luke Zechariah
Ranch Hand

Joined: Sep 27, 2005
Posts: 123
I have a bean which has objects such as
1. SSN
2. TransferDate
3. FirstName
4. City

We are displaying these values on a jsp page.
Now I am adding let's say 50 bean objects into a arraylist or a tree map or some list. How do I sort programatically which is done on the database usually.
Let's see this sql: "Select SSN, TransferDate, FirstName, City from Employee orderby TransferDate DESC, City ASC, FirstName DESC, SSN ASC"

How do I write a java code for such as the above programatically. I have searched intensely a solution for this. Please help me if anyone has a answer. I tried using the Comparator, Comparable interface but am not able to code properly. Any techinque is ok for even if it is by javascript.

Thanks in advance.
Luke.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19794
    
  20

Comparator is the still way to go.

If you have multiple fields it usually goes as follows:

Nesting the if-statements will improve speed a little bit* but I find that this improves readability.


* If diff will be non-zero after the first comparison, it will still check in all the other if-statements.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting of a Arraylist with multiple elements with sort order