Using Python Django I have a view that returns JSON to a template that initializes a global JavaScript variable as such:
<script type="text/javascript">
coordinates = {{ coordinates | safe}}
</script>
These coordinates have a field called country.
I sort these objects using:
coordinates.sort(sortByCountry);
My sortByCountry function:
function sortByCountry(x, y) {
return ((x.fields.country == y.fields.country) ?
0 : ((x.fields.country > y.fields.country) ? 1 : -1 ));
}
So when I run
coordinates.sort(sortByCountry);
it returns the correct order of objects.
But if I loop through coordinates it loops through as if it has not been sorted. Here for each country i correct a new array and push them in so all coordinates that belong to the same country are in the same array. All these arrays are nested in another array called countries.
var countries = [];
var counter = 0; // number of countries
function sortByCountry(x, y){
return ((x.fields.country == y.fields.country) ? 0 : ((x.fields.country > y.fields.country) ? 1 : -1 ));
}
function country_assignment(){
coordinates.sort(sortByCountry); // ****This works returns a sorted coordinates list, can even do window.coordinates and get the sorted list
countries.push(new Array());
countries[counter].push(coordinates.pop());
length = coordinates.length
for( var l = 0; l < length; l++){
//this loops through coordinates as if it was not sorted
if((countries[counter][0].fields.country == coordinates[0].fields.country)){
countries[counter].push(coordinates.pop());
} else {
countries.push(new Array());
counter = counter + 1;
countries[counter].push(coordinates.pop());
I've tried
coordinates = coordinates.sort(sortByCountry);
But this does not work as well.
Example of JSON objects:
<script type="text/javascript">
coordinates = [{"fields": {"latitude": 38.5512238, "country": "USA", "location": "802 D St, Davis, CA 95616, USA", "longitude": -121.7441921, "visited": true}, "model": "mapper.destination", "pk": 1}, {"fields": {"latitude": 51.501009, "country": "Britian", "location": "London SW1A 1AA, UK", "longitude": -0.1415876, "visited": true}, "model": "mapper.destination", "pk": 2}, {"fields": {"latitude": 51.501009, "country": "Britian", "location": "London SW1A 1AA, UK", "longitude": -0.1415876, "visited": true}, "model": "mapper.destination", "pk": 3}, {"fields": {"latitude": 13.7524008, "country": "Thailand", "location": "Na Phra Lan Rd, Khwaeng Phra Borom Maha Ratchawang, Khet Phra Nakhon, Krung Thep Maha Nakhon 10200, Thailand", "longitude": 100.490729, "visited": true}, "model": "mapper.destination", "pk": 4}, {"fields": {"latitude": 51.5073509, "country": "Britian", "location": "London, UK", "longitude": -0.1277583, "visited": true}, "model": "mapper.destination", "pk": 5}, {"fields": {"latitude": 51.1802192, "country": "Britian", "location": "Salisbury, Wiltshire SP4 7DE, UK", "longitude": -1.8270873, "visited": true}, "model": "mapper.destination", "pk": 6}, {"fields": {"latitude": 7.9519331, "country": "Thailand", "location": "Phuket, Thailand", "longitude": 98.33808839999999, "visited": true}, "model": "mapper.destination", "pk": 7}, {"fields": {"latitude": 25.7616798, "country": "USA", "location": "Miami, FL, USA", "longitude": -80.1917902, "visited": true}, "model": "mapper.destination", "pk": 8}]
</script>