If need to sort a object considering it attributes, we can do as below.
//Some class
private void sortList(){
// Sorting
Collections.sort(getParkedVehicles(), new Comparator<Vehicle>() {
@Override
public int compare(Vehicle vehicle2, Vehicle vehicle1)
{
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
return LocalDateTime.parse(vehicle1.getEntryTime(),formatter).compareTo( LocalDateTime.parse(vehicle2.getEntryTime(),formatter));
}
});
}
public class Vehicle {
private String idPlate;
private String brand;
private String entryTime;
public String getIdPlate() {
return idPlate;
}
public void setIdPlate(String idPlate) {
this.idPlate = idPlate;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getEntryTime() {
return entryTime;
}
public void setEntryTime(String entryTime) {
this.entryTime = entryTime;
}
}
More details:
http://stackoverflow.com/questions/18441846/how-to-sort-an-arraylist-in-java
No comments:
Post a Comment