Ali-Alrabi
Ali-Alrabi

Reputation: 1698

Want to sort map according object key

I have a map and his key and value are obejct like

    List<AppointmentRequest> list=AppointmentRequest.findAllAppointmentRequests();
    for(AppointmentRequest a:list){
        Store store=Store.findStore(a.getStoreId());
        map.put(AppointmentRequest.findAppointmentRequest(a.getId()),store);

    }

AppointmentRequest contain date column Now I want to sort this map according this column to show it in jsp page,Can anyone help me please?

Upvotes: 1

Views: 85

Answers (1)

Eran
Eran

Reputation: 393771

Use a TreeMap :

A Red-Black tree based NavigableMap implementation. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.

Supply a Comparator to the constructor in order to sort the keys by your date column.

It can look like that :

Map<AppointmentRequest,Store> map = 
    new TreeMap(new Comparator<? super AppointmentRequest> comparator {
        int compare(AppointmentRequest o1, AppointmentRequest o2) {
            // return -1,0 or 1 based on the relative order of o1 and o2
        }
    });

Upvotes: 1

Related Questions