I have a table with lat, long, location name. I have calculated the nearest distance from the current location and added the distance in one arraylist and then sorted in ascending order. Next i have added name in other arraylist. Now i need to set the names in the order using the distance from other list. How to do it?
5 Answers
I think you are using arrays too extensively here. If you have two or more pieces of related data, you should not put each piece into a separate array. Instead create a class to hold all the related data (lat, long, distance and name) and have an array (or some other collection that better meets your needs such as a SortedList) that holds instances of this class. The class could implement Comparable using distance as the measure and therefore allow sorting by distance.
Comments
are your distances calculated by latitude, longtitude in that table? then why not using a map data structure?
it provides you the pair location-distance
use Arrays.sort(). You can dont even have to add the distance of each one from current in a separate array. just implement the comparator correctly.
use this after doing a toArray() on your ArrayList.