aspose file tools*
The moose likes Java in General and the fly likes Sorting Collection of ArrayList 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 Collection of ArrayList" Watch "Sorting Collection of ArrayList" New topic
Author

Sorting Collection of ArrayList

Deepak Kumar
Ranch Hand

Joined: Nov 05, 2007
Posts: 62
Hi All,

How to sort a Collection of ArrayList based on the first(date)element of the ArrayList?

For example,(date is in YYYY/MM/DD format)
I have a Collection as: [[2002/02/01,12,34],[2005/02/08,45,67],[2001/02/11,78,91],[2006/01/27,78,34],[2006/01/28,88,45]]

where [2002/02/01,12,34],[2005/02/08,45,67],[2001/02/11,78,91][2006/01/27,78,34],[2006/01/28,88,45] are ArrayLists.

Now I want to sort the above collection on date by desc order as
[[2006/01/28,88,45],[2006/01/27,78,34],[2005/02/08,45,67],[2002/02/01,12,34],[2001/02/11,78,91]]

Please give me some clear idea on this?

Thanks& Regards,
Deepak


Thanks,
Deepak
Jeremy Wilson
Ranch Hand

Joined: Feb 18, 2003
Posts: 166
Wrap the arrays in their own object and implement the comparator interface. Then you can use Collections.sort to sort the array based on the method provided in the comparator implementation


Jeremy Wilson
Deepak Kumar
Ranch Hand

Joined: Nov 05, 2007
Posts: 62
Thanks Jeremy Wilson
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

Or use the Collections.sort method that takes a Comparator. That way you can keep using Strings.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting Collection of ArrayList