I am having a custom class called Company, and it has fields being latitude and longitude. I want to sort them based on distance from current location but it does not seem to work.
I have double checked all the data, and they are correct but for a reason the sorting does not seem to work.
Code is :
currentCompanies=AllCompanies.sortByDistanceFromOwnLocation(currentCompanies, AppConstant.userLatitude, AppConstant.userLongitude);
which calls these :
public static Vector<Company> sortByDistanceFromOwnLocation(
Vector<Company> locations, final double myLatitude,
final double myLongitude) {
Comparator comp = new Comparator<Company>() {
@Override
public int compare(Company o, Company o2) {
float[] result1 = new float[3];
android.location.Location.distanceBetween(myLatitude,
myLongitude,
Double.parseDouble(o.getLatitude().trim()),
Double.parseDouble(o.getLongitude().trim()),
result1);
Float distance1 = result1[0];
float[] result2 = new float[3];
android.location.Location.distanceBetween(myLatitude,
myLongitude,
Double.parseDouble(o2.getLatitude().trim()),
Double.parseDouble(o2.getLongitude().trim()),
result2);
Float distance2 = result2[0];
return distance1.compareTo(distance2);
}
};
Collections.sort(locations, comp);
return locations;
}
which calls this from the Collection.class:
/**
* Sorts the given list using the given comparator. The algorithm is
* stable which means equal elements don't get reordered.
*
* @throws ClassCastException if any element does not implement {@code Comparable},
* or if {@code compareTo} throws for any pair of elements.
*/
@SuppressWarnings("unchecked")
public static <T> void sort(List<T> list, Comparator<? super T> comparator) {
T[] array = list.toArray((T[]) new Object[list.size()]);
Arrays.sort(array, comparator);
int i = 0;
ListIterator<T> it = list.listIterator();
while (it.hasNext()) {
it.next();
it.set(array[i++]);
}
}
What am I doing wrong?
EDIT :
Ok more info on that. I am having 8 companies - lets say id from 1 to 8. I have added this log inside the compare method :
System.out.println("Compare between id :" + o.getId() + " and " + o2.getId());
And is being called only for these id's :
Compare between id :2 and 1
Compare between id :3 and 2
Data for all the companies are :
12-24 17:00:51.318: I/System.out(19041): Company : 1 Latitude :37.9407699 Longitude:23.7489698
12-24 17:00:51.318: I/System.out(19041): Company : 2 Latitude :37.9407699 Longitude:23.7489698
12-24 17:00:51.318: I/System.out(19041): Company : 3 Latitude :37.9407699 Longitude:23.7489698
12-24 17:00:51.319: I/System.out(19041): Company : 4 Latitude :41,352979 Longitude:26,502281
12-24 17:00:51.319: I/System.out(19041): Company : 5 Latitude :41,352979 Longitude:26,502281
12-24 17:00:51.319: I/System.out(19041): Company : 6 Latitude :41,352979 Longitude:26,502281
12-24 17:00:51.319: I/System.out(19041): Company : 7 Latitude :37.0861346 Longitude:25.1608451
12-24 17:00:51.319: I/System.out(19041): Company : 8 Latitude :38.078994 Longitude:23.727481
Result of the comparison between id 1 and 2 and 2 and 3 is 0 => equal (which is correct). The others are not compared at all.
Why is that?
Now we can see the string values for the latitude and longitude, I suspect I know the problem. Double.parseDouble
will throw an exception for a string like this: 41,352979
.
My guess is that somewhere you're catching Exception
and effectively ignoring it. That exception is being thrown during the sort
method, when the comparator tries to parse the invalid value.
Things to fix:
Company.getLatitude
and Company.getLongitude
should be double
values already; it shouldn't be up to the user to trim strings and parse themSee more on this question at Stackoverflow