sorting list of Object class before adding the list into the gridModel
The code is given below .........
private List<EmployeeAllRec> listg;
private List<Employee> list;
private List<Employee> gridModel;
private Map<String, String> json;
public String showAllRecord() {
records = 30;
rows = 1开发者_高级运维0;
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
EmployeeAllRec rs = null;
try {
transaction = session.beginTransaction();
listg = new ArrayList();
List emp = session.createQuery("from Employee e").list();
int c=0;
//code for adding the data into the list
for (Iterator iterator = emp.iterator(); iterator.hasNext();) {
Employee e1 = (Employee) iterator.next();
System.out.println(e1.getName());
rs = new EmployeeAllRec();
rs.setName(e1.getName());
rs.setEmail(e1.getEmail());
rs.setDob(e1.getDob());
rs.setAddress(e1.getAddress());
rs.setGender(e1.getGender());
rs.setAge(e1.getAge());
rs.setCountry(e1.getCountry());
rs.setContact(e1.getContact());
rs.setWebsite(e1.getWebsite());
System.out.println("&&&&&&&&&&&&&&&&---i m on it " + rs.getName());
listg.add(rs);
}
setGridModel(listg);
// some stuff
My problem is that I need to sort the "listg" variable into descending order before adding it into the variable setGridModel....
Since the objects that are added into listg are taken from a DB, I would suggest ordering it through the HQL query.DB ordering is faster then in memory ordering (using a comparator), and in this case you can apply approach.
So the HQL query will look smth like "FROM Employee e ORDER BY... DESC"
- Define a compareTo on your object that sorts them in descending order.
- Call Collections.sort(listg) once it's populated.
PS: Consider using generics in your collections if possible.
精彩评论