在这里,我根据与当前位置的距离找出所有排序的实体列表。
当我将任何常量值代替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)
你能帮我解决这个问题吗
最佳答案
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() a.getShortedDistance()> b.getShortedDistance()吗? 1:0;
希望能帮助到你。
关于java - 使用IllegalArgumentException的比较器进行集合排序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34138119/