开发者社区> 问答> 正文

Android Collections排序相关问题

#下面代码Collections中如何根据照片的经纬度来进行排序?# 
    /**
     * 按照地点进行分组
     * @param context
     * @param list
     * @return
     */
    public static List<GroupEntity> setAddressOrder(Context context, List<ParentEntity> list) {
        if (list == null || list.isEmpty()) {
            return null;
        }

        //排序前
        for (int i = 0; i < list.size(); i++) {
            String address = LocationUtil.queryLatLongItudeLocation(context,
                    list.get(i).getLatitude(), list.get(i).getLongitude());
            if(address != null){
                Log.e("===>>>", address);
            }
        }

        Collections.sort(list, new Comparator<ParentEntity>() {
            @Override
            public int compare(ParentEntity lhs, ParentEntity rhs) {
                if(1 < Math.abs(lhs.getLatitude() - rhs.getLatitude()) 
                        && 1 < Math.abs(lhs.getLongitude() - rhs.getLongitude())){
                    return 2;
                }else{
                    return 0;
                }
            }
        });

        //排序后
        for (int i = 0; i < list.size(); i++) {
            String address = LocationUtil.queryLatLongItudeLocation(context,
                    list.get(i).getLatitude(), list.get(i).getLongitude());
            if(address != null){
                Log.e("===>>>", address);
            }
        }

        List<GroupEntity> entlist = new ArrayList<GroupEntity>();
//      GroupEntity group = null;//用来存储数据
//      for (int i = 0; i < list.size(); i++) {
//          ParentEntity entity = list.get(i);
//          
//          if (group == null 
//                  || 1 < Math.abs(group.latitude - entity.getLatitude()) 
//                  && 1 < Math.abs(group.longitude - entity.getLongitude())) {
//              group = new GroupEntity();//不相同新建放入
//              group.latitude = entity.getLatitude();
//              group.longitude = entity.getLongitude();
//              entlist.add(group);
//          }
//          group.list.add(entity);
//      }
        return entlist;
    }

展开
收起
爵霸 2016-03-25 11:08:38 1854 0
1 条回答
写回答
取消 提交回答
  •  /**
         * 按照地点进行分组
         * 
         * @param context
         * @param list
         * @return
         */
        public static List<GroupEntity> setAddressOrder(Context context,
                List<ParentEntity> list) {
            if (list == null || list.isEmpty()) {
                return null;
            }
            List<ParentEntity> temp = new ArrayList<ParentEntity>(list);
            List<GroupEntity> result = new ArrayList<GroupEntity>();
            while (!temp.isEmpty()) {
                GroupEntity ge = new GroupEntity(temp.remove(0));
                for (ParentEntity e : temp) {
                    if (0.5 > Math.abs(ge.latitude - e.getLatitude())
                            && 0.5 > Math.abs(ge.longitude - e.getLongitude())) {
                        ge.list.add(e);
                    }
                }
                temp.removeAll(ge.list);
                result.add(ge);
            }
            return result;
        }
    2019-07-17 19:13:54
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
相关产品:
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载