Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Sorting a list of object using comparator

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a list of Objects. The Object has a Map and the Map has a key lastReplicatedDate. I need to sort the list of Object using this lastReplicatedDate in descending order. How do I achieve this. I want to implement is using Collections.sort(List, Comparator) and hence want to write a Comparator for this.

Thanks in advance,
Ankit
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'll have to create a class that implements the Comparator interface for the type of the objects that you want to compare, and then in the compareTo() method compare the two objects that you get as arguments using the rules that you described.

Try it yourself - and if you get stuck, please post your code and explain what the problem is.
 
Marshal
Posts: 79645
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a section about ordering in the Java Tutorials. I think that would be helpful
 
reply
    Bookmark Topic Watch Topic
  • New Topic