Java: Order a list of Objects based on Calendar date?

Tags: , , ,



I have a list of Java Objects that all contain the following field:

@JsonProperty
@Column(name = "date")
private Calendar date;

I want to order the list of objects based on the above field , the object with date field most recent first.

What is the best way to do so?

Answer

You can use Comparator.comparing, suppose you have List<Entity> then you can do this way

objList.sort(Comparator.comparing(Entity::getDate).reversed());


Source: stackoverflow