梅明子 2015-11-30 02:28 采纳率: 100%
浏览 2091
已采纳

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;
    }
  • 写回答

1条回答 默认 最新

  • 梅明子 2015-11-30 06:04
    关注

    自己解决!!!

        /**
         * 按照地点进行分组
         * 
         * @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;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)