Reputation: 31
How can I order by descending and take max results and at end I want list to sort ascending. I have the following code which is not working
Criteria cr = sessionFactory.getCurrentSession().createCriteria(Employee.class)
.add(Restrictions.eq("empId", empId)).add(Restrictions.eq("empCode", empCode));
cr.addOrder(Order.desc("joinDate"));
cr.setMaxResults(5);
cr.addOrder(Order.asc("joinDate"));
return cr.list();
Upvotes: 1
Views: 1311
Reputation: 1
this is for when you are use List
List<Incident> sortedIncidents = employee.getIncidents().stream() .sorted(Comparator.comparing(Incident::getCreatedOn).reversed())
.collect(Collectors.toList());
this is for when you are use Set
Set<Incident> sortedIncidents = new TreeSet<>(
(incident1, incident2) -> incident2.getCreatedOn().compareTo(incident1.getCreatedOn()));
Upvotes: 0
Reputation: 31
noOfEmployee is the list return from the above criteria which you should initialize and Employee is the model class.
Collections.sort(noOfEmployee, new Comparator<Employee>(){
@Override
public int compare(Employee o1, Employee o2) {
// TODO Auto-generated method stub
return o1.getJoinDate().compareTo(o2.getJoinDate());
}
});
Upvotes: 1