File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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: 3371
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.

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 ]

[] | [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 - -
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
I agree. Here's the link:
subject: Sorting my list
jQuery in Action, 3rd edition