aspose file tools*
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
Author

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
Author
Ranch Hand

Joined: May 31, 2007
Posts: 3248
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.



http://java.sun.com/j2se/1.4.2/docs/api/java/util/Collections.html#sort(java.util.List)

http://www.java2s.com/Tutorial/Java/0140__Collections/SortingaList.htm

[ 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.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting my list