Reputation: 188
Here I am finding out all the sorted list of entity on the basis of it's distance from the current location.
When i put any constant value in place of ebeeRoutePlansList.get(i).getRetailerLatitude() then collection.sort get work well . Is there any problem with my data inside latitude or other things. Here is link of my api http://52.74.125.208:9080/routeplanuser/getUserVisitDetails/1
from where we get latitude and longitude and saved in 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);
}
I have used Collections.sort(ebeeRoutePlansList, new DistanceComparator());
the DistanceComparator class having
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;
}
}
First this ouput get print
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
then the exception occur after last line get executed
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)
Can you please help me to sort out this problem
Upvotes: 0
Views: 107
Reputation: 988
Why use a Comparator
if all you ought to do is check if they are null? dont you think you are complicating things a bit too much? chances your are trying to re-define the law of gravity try to look at this https://stackoverflow.com/a/11441813/5590309 answer it is pretty much self explanatory
Upvotes: 0
Reputation: 10142
what is the data type of a.getShortedDistance()
? Is it double? comparing a double for equality is a mistake , you should not do ,
a.getShortedDistance() ==b.getShortedDistance()
. Replace second inner condition with ,
a.getShortedDistance() > b.getShortedDistance()?1:0
Also, you get exception because method violates transitivity. Lets say a.getShortedDistance()= null
and b.getShortedDistance() = 10
and c.getShortedDistance()=20
. Your method makes a=b and a=c but b != c and that is wrong. If a==b and b!=c then a !=c.
I guess , your correct method should simply do this,
return a.getShortedDistance() < b.getShortedDistance() ? -1 : a.getShortedDistance() > b.getShortedDistance() ? 1 : 0;
Hope it helps.
Upvotes: 1
Reputation: 3893
I guess using equals()
rather than ==
might fix it.
Double a = 2d;
Double b = 2d;
System.out.println(a == b); // false
System.out.println(a.equals(b)); // true
When a and b are equal, but ==
is false
, then both compare(a, b)
and compare(b, a)
will return 1, and so the exception is thrown.
Or you could just return a.getShortedDistance().compareTo(b.getShortedDistance())
.
Upvotes: 0