實體對象數據根據百度地圖地理位置排序解決方法

1,先要寫一個比較距離大小的工具類java

/**
/**  
 * @Description: TODO(用一句話描述該文件作什麼)
 * @author daipengcheng 
 * @date 2015-9-2 下午3:37:38
 * @version V1.0 
 */

// 
public class LTCompare implements Comparator<qyxxb> {
 @Override
 public int compare(qyxxb1 01, qyxxb o2) {
  // TODO Auto-generated method stub
  //距離字段存在該實體類中 
  double a1=Double.parseDouble(o1.getjl());
  double a2=Double.parseDouble(o2.getjl());
  if(a1>a2)
   return 1;
  else if(a1==a2){
   if(o1.getid().compareTo(o2.getid())==1)
    //o1.getid().compareTo(o2.getid())
    //Integer.parseInt(IsNum(o1.getid()))>Integer.parseInt(IsNum(o2.getid()))
    return 1;
   else if(o1.getid()==o2.getid())
    return 0;
   else
    return -1;
  }
  else
   return -1;
 }
 private String IsNum(String str)
 {
  Pattern pattern = Pattern.compile("[^0-9]");
  Matcher matcher = pattern.matcher(str);
  String all = matcher.replaceAll("");
  return all;
 }
}

//2 後面就會使用該工具類了
//這裏聲明瞭一個工具類,這個工具類中存放企業信息呀
public static List<qyxxb> list = new ArrayList<qyxxb>() ; 
//先聲明該工具類 
  LTCompare comparator=new LTCompare();
//將該工具類傳入到Collections集合中進行比較
  Collections.sort(dpclt_utils.list, comparator);
  list.addAll(dpclt_utils.list) ;
  adapter.notifyDataSetChanged();
相關文章
相關標籤/搜索