Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Order a collection by date

 
John Landon
Ranch Hand
Posts: 239
  • 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: 239
  • 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
Sheriff
Posts: 22542
109
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: 230
  • 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: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. Just figured it out.
 
No more fooling around. Read this tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!