Skip to content
Advertisement

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

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?

Advertisement

Answer

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

objList.sort(Comparator.comparing(Entity::getDate).reversed());
3 People found this is helpful
Advertisement