File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Sorting List according to Date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sorting List according to Date" Watch "Sorting List according to Date" New topic
Author

Sorting List according to Date

Kunal Sathe
Ranch Hand

Joined: Apr 08, 2005
Posts: 49
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

Joined: Feb 24, 2006
Posts: 6662
    
    5

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


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Kunal Sathe
Ranch Hand

Joined: Apr 08, 2005
Posts: 49
Thanks John!

That seems to have worked!

Really appreciate the quick response.

Kunal
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Sorting List according to Date