Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting my list

 
shaf maff
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Renato Losio
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not sorting them when you query from the DB?
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This type of sorting is best done via your SQL when you do your select.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic