• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Order a collection by date

 
John Landon
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
What is the best way to order a Collection of ClassA by date. The date is stored in ClassA (like new ClassA().date)?

Thanks.
 
John Landon
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a Collections.public static void sort(List list, Comparator c).
But what do I give it as c?
 
Henry Wong
author
Marshal
Pie
Posts: 21024
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by John Landon:
There is a Collections.public static void sort(List list, Comparator c).
But what do I give it as c?


This is hardly an advanced questio. Perhaps this should be moved to beginners?

Anyway to answer your question. How about something like this?



Henry
 
Paul Yule
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you need to create a class that implements the comparator...take a look at the comparator interface (2 methods). Or create an anonymous comparator that implements compare() on the spot:
 
Tom Johnson
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Give it a comparator....what else?!! You can use a named implementation or pass an anonymous implementation, as follows:



Hope this helps
[ November 20, 2008: Message edited by: Tom Johnson ]
 
John Landon
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. Just figured it out.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic