• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sort ArrayList based on date

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai,
i want to display dates stored in an ArrayList in descending order can any one help me on how to do this .
Should i write a custom method for this ? or can i do it with Comparator or Comparable class .
Thanks and regards
Anuja Karthikeyan
 
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Collections.sort() would sort the list in normal order, running that result through Collections.reverse() (after converting it back into a List) would give you the reverse order List.
Or indeed use a Comparator which does the reverse sorting for you as an argument to Collections.sort().

See the Javadoc for java.util.Collections for details.
 
anuja Edathu
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Jeroen T Wenting ,
But if my ArrayList contains objects and i want to sort these object according to the date field how can i go do this ?
Regards ,
Anuja Karthikeyan
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Write a class that implements interface Comparator for your objects and use it in a call to Collections.sort().

Make the Comparator class compare your objects by date.
[ April 28, 2006: Message edited by: Jesper Young ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic