代码之家  ›  专栏  ›  技术社区  ›  Ali-Alrabi

要根据对象键对地图进行排序

  •  1
  • Ali-Alrabi  · 技术社区  · 10 年前

    我有一张地图,他的钥匙和价值观都很好

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

    约会请求包含日期列 现在我想根据这个列对这个地图进行排序,以便在jsp页面中显示它,有人能帮我吗?

    1 回复  |  直到 10 年前
        1
  •  1
  •   Eran    10 年前

    使用 TreeMap :

    基于红黑树的NavigableMap实现。地图根据其键的自然顺序进行排序,或者由地图创建时提供的Comparator进行排序,具体取决于使用的构造函数。

    向构造函数提供一个Comparator,以便按日期列对键进行排序。

    它可能看起来像这样:

    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
            }
        });