• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sorting Collection of ArrayList

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Deepak Kumar
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jeremy Wilson
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or use the Collections.sort method that takes a Comparator. That way you can keep using Strings.
 
and POOF! You're gone! But look, this tiny ad is still here:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic