• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sorting a list of object using comparator  RSS feed

 
Ankit Chandrawat
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • 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
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Marshal
Posts: 56545
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a section about ordering in the Java Tutorials. I think that would be helpful
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!