Reputation: 1671
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
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
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