So I am developing a lost and found app using Fabric API. It has a option to sort the collected tweets based on user's current location. I found the below way online to sort using a comparator. This however, doesn't seem to work and the before-sorting and after-sorting results are totally alike.
public class SortLocations implements Comparator<Tweet> {
Double currLat;
Double currLng;
public SortLocations(Double currLat1, Double currLng1) {
currLat = currLat1;
currLng = currLng1;
}
@Override
public int compare(final Tweet tweet1, final Tweet tweet2) {
double lat1 = 0, lon1 = 0, lat2 = 0, lon2 = 0, distanceToPlace1 = 0, distanceToPlace2 = 0;
try {
lat1 = tweet1.coordinates.getLatitude();
lon1 = tweet1.coordinates.getLongitude();
lat2 = tweet2.coordinates.getLatitude();
lon2 = tweet2.coordinates.getLongitude();
distanceToPlace1 = distance(currLat, currLng, lat1, lon1);
distanceToPlace2 = distance(currLat, currLng, lat2, lon2);
} catch (Exception E) {
Log.d("No coordinates", "");
}
return (int) (distanceToPlace1 - distanceToPlace2);
}
public double distance(double fromLat, double fromLon, double toLat, double toLon) {
double radius = 6378137; // approximate Earth radius, *in meters*
double deltaLat = toLat - fromLat;
double deltaLon = toLon - fromLon;
double angle = 2 * Math.asin(Math.sqrt(
Math.pow(Math.sin(deltaLat / 2), 2) +
Math.cos(fromLat) * Math.cos(toLat) *
Math.pow(Math.sin(deltaLon / 2), 2)));
return radius * angle;
}
}
This is how the class is used in my activity
Collections.sort(tweetsSortedByLocation, new SortLocations(currLat, currLng));
Where tweetsSortedByLocation is of type List. Any help is really appreciated :)