-2

I want to sort a list of Booking objects which was retrieved from a database. I want to sort this list and save it to orderedList. I then want to create another list which stores the last 10 bookings and next 10 bookings from todays date.

How can I do this?

Date now = new Date();
    Session session = User.getSession();
    Transaction transaction = null;

    try {
        transaction = session.beginTransaction();

        String hql = "FROM Booking";
        Query query = session.createQuery(hql);
        List<Booking> bookingList = query.list();
        List<Booking> orderedList;

        for(int i =0; i<bookingList.size();i++){

        }

        transaction.commit();
Ravi
  • 30,829
  • 42
  • 119
  • 173
  • Why can't you sort on database itself and retrive? – Vasu Mar 26 '17 at 16:54
  • *I then want to create another list which stores the last 10 bookings and next 10 bookings from todays date* stores where ? in object or database ? – Ravi Mar 26 '17 at 16:55
  • Possible duplicate of [How to use Comparator in Java to sort](http://stackoverflow.com/questions/2839137/how-to-use-comparator-in-java-to-sort) – Joe C Mar 26 '17 at 16:56
  • I want to sort the objects in the in bookingList and store it in the orderList above. I then want to create a new list storing the last 10 and next 10 bookings from todays date. – Ali Prince Mar 26 '17 at 16:57

1 Answers1

0

You can sort your result when retrieving from database itself

String hql = "FROM Booking ORDER BY column_name";

Sorting with Hibernate

Ravi
  • 30,829
  • 42
  • 119
  • 173