Valten1992
Newbie
One feature my app will have is the ability to sort a list of properties by how close they are to the users current position. I tried implementing a comparator but it does not seem to work.
In my attempt it takes two property objects, gets thier distance from the user and then compares that to each other, with the closest pushed closer to the front of the array. if anyone can see where I am going wrong with this I'll be grateful. i can do comparators with string arrays and the like but its been a long time since I did one for objects.
In my attempt it takes two property objects, gets thier distance from the user and then compares that to each other, with the closest pushed closer to the front of the array. if anyone can see where I am going wrong with this I'll be grateful. i can do comparators with string arrays and the like but its been a long time since I did one for objects.

Code:
Collections.sort(res, new Comparator<Property>()
{
@Override
public int compare(Property p1, Property p2) {
double la1 = p1.getLat();
double la2 = p2.getLat();
double lon1 = p1.getLon();
double lon2 = p2.getLon();
double dLat = Math.toRadians(lati - la1);
double dLon = Math.toRadians(longi - lon1);
//determine distance from first point
double a =
(Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(Math.toRadians(la1))
* Math.cos(Math.toRadians(lati)) * Math.sin(dLon / 2) * Math.sin(dLon / 2));
p1.setDist(a);
double dLat2 = Math.toRadians(lati - la2);
double dLon2 = Math.toRadians(longi - lon2);
//determine distance from second point
double a2 =
(Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(Math.toRadians(la2))
* Math.cos(Math.toRadians(lati)) * Math.sin(dLon / 2) * Math.sin(dLon / 2));
p2.setDist(a2);
int res2 = (int)p2.getDist();
int res = (int)p1.getDist();
return res2 - res;
}
});
PrototypeActivity.adapter.notifyDataSetChanged();
}
});
