-2

I have a Map< String,ArrayList< BonusEntity > > Here key is EmpId of Employee and key will contains the List< BonusEntity >

BonusEntity will contains the following fields(All fields are String)

Empid BonusDate BonusAmount Dept

I need to sort the ArrayList in Map based on the BonusDate descending order,Can anyone help Thanks in advance.

user1550575
  • 55
  • 2
  • 7
  • 1
    Please improve your question quality. You can find more tips in: [**How do I ask a good question?**](https://stackoverflow.com/help/how-to-ask) and [**How to create a Minimal, Complete, and Verifiable example**](https://stackoverflow.com/help/mcve) pages. – Szymon Stepniak Jul 28 '17 at 06:46
  • Entity class can implement Comparable implement Compare to as @Override public int compareTo(BonusEntity o) { return o.bonusdate.compareTo(this.bonusdate); } and sort list using Collections.sort(list); keep in mind that using string for holding date value will give you some unexpected results, use Date class instead of string – Ganesh Karewad Jul 28 '17 at 07:00

1 Answers1

0

You should create a Comparator that applies to your class BonusEntity and use it via the Collections.sort method.

Loic Mouchard
  • 1,121
  • 7
  • 22
  • 1
    Alternatively, have `BonusEntity` implement `Comparable` then call `List.sort()` – killjoy Jul 28 '17 at 06:51
  • Also possible, with `Comparable` you'll define one natural sort for the class. You can define as many `Comparator` as you can need/imagine. – Loic Mouchard Jul 28 '17 at 06:56