Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting List according to Date

 
Kunal Sathe
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have a list of Objects. These objects contain the effective date. I want to sort this list of objects and reorder it according to the date. The following is the piece of code I am referring to:

public List getPremiumSchedulesList() {

List returnList = new ArrayList();
Date date = null;

Vector altPremScheduleVector = getAltPremScheduleVector();
Iterator altPremScheduleVectorIterator =
altPremScheduleVector.iterator();
PremiumSchedule premiumSchedule = null;
while (altPremScheduleVectorIterator.hasNext()) {
premiumSchedule = new PremiumSchedule();
AltPremMode altPremMode = (AltPremMode) altPremScheduleVectorIterator.next();
premiumSchedule = populatePremiumSchedule(altPremMode);
date = premiumSchedule.getEffectiveDate();
System.out.println("Effective Date: "+date);
returnList.add(premiumSchedule);
}

return returnList;

}

The premiumSchedule object contains an effective date. I want to sort this list according to the effective date. I would really apprecite if someone can point me in the right direction

Looking forward to your responses.

Thanks,
Kunal
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at the Comparator and Comparable interface. You can implement the "compare" function for your class and use one of the overloaded Collections.sort() method
 
Kunal Sathe
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks John!

That seems to have worked!

Really appreciate the quick response.

Kunal
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic