Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting of a Arraylist with multiple elements with sort order

 
Luke Zechariah
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic