jQuery in Action, 2nd edition
The moose likes Servlets and the fly likes Sorting my list Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Sorting my list" Watch "Sorting my list" New topic

Sorting my list

shaf maff
Ranch Hand

Joined: Sep 07, 2008
Posts: 180
Hi Guys,

I have a list which containers mail orders from the db which are stored in a session. What Ive noticed when I view the orders on the webpage is that it starts from the oldest date to the newest. I want to switch this around. Is there any way I can do this? See basic structure of list below:

arulk pillai
Ranch Hand

Joined: May 31, 2007
Posts: 3322
use a compareTo method to sort in reverse order i.e. latest first.

implement a compareTo method in your Mail class. Make sure that it implements the Comparable interface. http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Comparable.html

You can then sort it as shown below. Your compareTo method gets called. Fopr more complex sorting, write your own Comparator. Check the Java API.



[ September 25, 2008: Message edited by: arulk pillai ]

[ September 25, 2008: Message edited by: arulk pillai ]
[ September 25, 2008: Message edited by: arulk pillai ]

[500+ Java Interview Questions and Answers ] | [Java job hunting know how] | [Java resumes]
Renato Losio
Ranch Hand

Joined: Nov 23, 2005
Posts: 99
Why not sorting them when you query from the DB?

Renato Losio - www.arsenio.it - renatoweb@arsenio.it
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
This type of sorting is best done via your SQL when you do your select.

So much trouble in the world -- Bob Marley
It is sorta covered in the JavaRanch Style Guide.
subject: Sorting my list