2015-12-07 3 views
0

Здесь я узнаю весь отсортированный список сущностей на основе его расстояния от текущего местоположения. Когда я помещаю любое постоянное значение вместо ebeeRoutePlansList.get (i) .getRetailerLatitude(), тогда collection.sort будет работать хорошо. Есть ли какие-либо проблемы с моими данными внутри широты или другими вещами. Вот ссылка на мой апи http://52.74.125.208:9080/routeplanuser/getUserVisitDetails/1 откуда мы получаем широту и долготу и сохранены в EbeeRouteplanlistСортировка коллекции с предоставлением компаратора IllegalArgumentException

for(int i = 0; i < ebeeRoutePlansList.size(); i++) 
     { 
      double dLat = Math.toRadians(currentLatitute-ebeeRoutePlansList.get(i).getRetailerLatitude()); 
      double dLng = Math.toRadians(currentlongitude-ebeeRoutePlansList.get(i).getRetailerLongitude()); 
      double a = Math.sin(dLat/2) * Math.sin(dLat/2) + 
        Math.cos(Math.toRadians(28.459267)) * Math.cos(Math.toRadians(currentLatitute)) * 
          Math.sin(dLng/2) * Math.sin(dLng/2); 
      double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
      dist = (float)(earthRadius * c); 
      Log.d(TAG,"the distance of each lat Long ==="+dist+"name "+ebeeRoutePlansList.get(i).getRetailerName()); 
      ebeeRoutePlansList.get(i).setShortedDistance(dist); 

     } 

я использовал Collections.sort(ebeeRoutePlansList, new DistanceComparator());

класс DistanceComparator имеющий

class DistanceComparator implements Comparator<EbeeRoutePlan> { 
     @Override 
     public int compare(EbeeRoutePlan a, EbeeRoutePlan b) { 
      Log.d(TAG,"the compare method inside Distance Comparator "+a.getShortedDistance()+" --- "+b.getShortedDistance()); 

      if(a.getShortedDistance() == null || b.getShortedDistance() == null) 
      { 
       return 0; 
      } 

      return a.getShortedDistance() < b.getShortedDistance() ? -1 : a.getShortedDistance() == b.getShortedDistance() ? 0 : 1; 
     } 
    } 

Сначала это Ouput получить печать

12-07 21:37:20.542 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.542 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.542 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.542 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.542 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.542 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.542 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.542 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273 --- 8.636234 
12-07 21:37:20.542 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273 --- 8.636234 
12-07 21:37:20.542 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273 --- 8.636234 
12-07 21:37:20.542 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273 --- 8.636234 
12-07 21:37:20.542 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273 --- 8.636234 
12-07 21:37:20.542 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.552 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.552 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.552 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.552 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.552 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.552 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.552 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.552 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.552 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.552 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.552 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.552 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.552 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.552 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.552 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.552 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.552 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.552 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.552 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.552 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.562 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.562 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.562 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.562 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273 --- 8.636234 
12-07 21:37:20.562 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273 --- 8.636234 
12-07 21:37:20.562 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273 --- 8.636234 
12-07 21:37:20.562 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273 --- 8.636234 
12-07 21:37:20.562 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273 --- 8.636234 
12-07 21:37:20.562 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273 --- 0.09162273 
12-07 21:37:20.562 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273 --- 8.636234 
12-07 21:37:20.562 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273 --- 0.09162273 
12-07 21:37:20.562 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273 --- 8.636234 
12-07 21:37:20.562 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273 --- 0.09162273 
12-07 21:37:20.562 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273 --- 0.09162273 
12-07 21:37:20.562 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273 --- 0.09162273 
12-07 21:37:20.562 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273 --- 8.636234 
12-07 21:37:20.562 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273 --- 8.636234 
12-07 21:37:20.562 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 0.09162273 
12-07 21:37:20.562 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.562 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.562 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 0.09162273 
12-07 21:37:20.562 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.562 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.572 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.572 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.572 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.572 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.572 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.572 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.572 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.572 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.572 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.572 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.572 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.572 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.572 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.572 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.572 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.572 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.572 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.572 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.572 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.572 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273 --- 0.09162273 
12-07 21:37:20.572 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273 --- 8.636234 
12-07 21:37:20.572 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.572 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273 --- 8.636234 
12-07 21:37:20.572 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273 --- 8.636234 
12-07 21:37:20.572 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273 --- 8.636234 
12-07 21:37:20.572 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 0.09162273 --- 8.636234 
12-07 21:37:20.572 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.582 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.582 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.582 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.582 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.582 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.582 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.582 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.582 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.582 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 
12-07 21:37:20.582 25683-25683/com.ebee.mobile.nativeandroid D/CartPageActivity﹕ the compare method inside Distance Comparator 8.636234 --- 8.636234 

то исключение происходит после последней строки получить выполнен

12-07 21:37:20.582 25683-25683/com.ebee.mobile.nativeandroid W/System.err﹕ java.lang.IllegalArgumentException: Comparison method violates its general contract! 
12-07 21:37:20.582 25683-25683/com.ebee.mobile.nativeandroid W/System.err﹕ at java.util.TimSort.mergeLo(TimSort.java:743) 
12-07 21:37:20.592 25683-25683/com.ebee.mobile.nativeandroid W/System.err﹕ at java.util.TimSort.mergeAt(TimSort.java:479) 
12-07 21:37:20.592 25683-25683/com.ebee.mobile.nativeandroid W/System.err﹕ at java.util.TimSort.mergeCollapse(TimSort.java:406) 
12-07 21:37:20.592 25683-25683/com.ebee.mobile.nativeandroid W/System.err﹕ at java.util.TimSort.sort(TimSort.java:210) 
12-07 21:37:20.592 25683-25683/com.ebee.mobile.nativeandroid W/System.err﹕ at java.util.TimSort.sort(TimSort.java:169) 
12-07 21:37:20.592 25683-25683/com.ebee.mobile.nativeandroid W/System.err﹕ at java.util.Arrays.sort(Arrays.java:2038) 
12-07 21:37:20.592 25683-25683/com.ebee.mobile.nativeandroid W/System.err﹕ at java.util.Collections.sort(Collections.java:1891) 
12-07 21:37:20.592 25683-25683/com.ebee.mobile.nativeandroid W/System.err﹕ at com.ebee.mobile.nativeandroid.activities.cartpage.CartPageActivity.getSortedDistanceRetailerList(CartPageActivity.java:853) 
12-07 21:37:20.592 25683-25683/com.ebee.mobile.nativeandroid W/System.err﹕ at com.ebee.mobile.nativeandroid.activities.cartpage.CartPageActivity$1.run(CartPageActivity.java:188) 
12-07 21:37:20.592 25683-25683/com.ebee.mobile.nativeandroid W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:725) 
12-07 21:37:20.592 25683-25683/com.ebee.mobile.nativeandroid W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:92) 
12-07 21:37:20.592 25683-25683/com.ebee.mobile.nativeandroid W/System.err﹕ at android.os.Looper.loop(Looper.java:176) 
12-07 21:37:20.592 25683-25683/com.ebee.mobile.nativeandroid W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5317) 
12-07 21:37:20.592 25683-25683/com.ebee.mobile.nativeandroid W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method) 
12-07 21:37:20.592 25683-25683/com.ebee.mobile.nativeandroid W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:511) 
12-07 21:37:20.592 25683-25683/com.ebee.mobile.nativeandroid W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 
12-07 21:37:20.592 25683-25683/com.ebee.mobile.nativeandroid W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
12-07 21:37:20.592 25683-25683/com.ebee.mobile.nativeandroid W/System.err﹕ at dalvik.system.NativeStart.main(Native Method) 

Можете ли вы помочь мне разобраться в этой проблеме

+0

Проверьте это [ссылка] (http://stackoverflow.com/questions/8327514/comparison-method- нарушает его, генерал-контракт). Это определенно поможет вам. –

ответ

0

Я думаю, используя equals() вместо == может это исправить.

Double a = 2d; 
    Double b = 2d; 
    System.out.println(a == b);  // false 
    System.out.println(a.equals(b)); // true 

Когда и б равны, но == является false, то оба compare(a, b) и compare(b, a) будет возвращать 1, и поэтому исключение.

Или вы могли бы просто вернуть a.getShortedDistance().compareTo(b.getShortedDistance()).

1

какой тип данных a.getShortedDistance()? Это двойное? сравнивая двойной для равенства является ошибкой, вы не должны делать,

a.getShortedDistance() == b.getShortedDistance()

. Заменить второе внутреннее состояние с,

a.getShortedDistance()> b.getShortedDistance() 1: 0

Кроме того, вы получите исключение, потому что метод нарушает транзитивность. Давайте скажем a.getShortedDistance()= null и b.getShortedDistance() = 10 и c.getShortedDistance()=20. Ваш метод делает a = b и a = c, но b! = C, и это неверно. Если a == b и b! = C, то a! = C.

Я думаю, ваш правильный метод должен просто сделать это,

возвращение a.getShortedDistance() < b.getShortedDistance()? -1: a.getShortedDistance()> b.getShortedDistance()? 1: 0;

Надеюсь, это поможет.

0

Зачем использовать Comparator, если все, что вам нужно сделать, это проверить, являются ли они пустыми? Не думаете ли вы, что вы слишком много усложняете вещи?шансы, что вы пытаетесь переформулировать закон гравитации, попытаются посмотреть на это https://stackoverflow.com/a/11441813/5590309 Ответ на это очень много объяснений

Смежные вопросы