在这里,我根据它与当前位置的距离找出所有实体的排序列表。
当我用任何常量值代替ebeeRoutePlansList.get(i).getRetailerLatitude()时,collection.sort就可以正常工作了。我的数据在纬度或其他内容是否有任何问题。这是我的api http://52.74.125.208:9080/routeplanuser/getUserVisitDetails/1
的链接
从我们获得纬度和经度并保存在EbeeRouteplanlist
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;
}
}
首先这个输出得到打印
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 :(得分:1)
a.getShortedDistance()
的数据类型是什么?它加倍了吗?比较双重的平等是一个错误,你不应该这样做,
a.getShortedDistance()== b.getShortedDistance()
。用,
替换第二个内部条件a.getShortedDistance()&gt; 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()&lt; b.getShortedDistance()? -1: a.getShortedDistance()&gt; b.getShortedDistance()? 1:0;
希望它有所帮助。
答案 1 :(得分:0)
我猜使用equals()
而非==
可能会修复它。
Double a = 2d;
Double b = 2d;
System.out.println(a == b); // false
System.out.println(a.equals(b)); // true
当a和b相等但==
为false
时,compare(a, b)
和compare(b, a)
都将返回1,因此抛出异常。
或者您可以返回a.getShortedDistance().compareTo(b.getShortedDistance())
。
答案 2 :(得分:0)
为什么使用Comparator
如果你应该做的就是检查它们是否为空?难道你不觉得你的事情太复杂了吗?你试图重新定义万有引力定律的机会试着看看这个https://stackoverflow.com/a/11441813/5590309答案它是非常自我解释的