java12399900
java12399900

Reputation: 1671

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?

Upvotes: 1

Views: 834

Answers (2)

Eritrean
Eritrean

Reputation: 16498

Sort by date ascending

yourList.sort(Comparator.comparing(YourObject::getDate));

Sort by date descending

yourList.sort(Comparator.comparing(YourObject::getDate).reversed());

Upvotes: 2

Eklavya
Eklavya

Reputation: 18430

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

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

Upvotes: 3

Related Questions