Reputation: 453
I have a JSON array:
var details = [
{
'address':'Pantaloons,701-704, 7th Floor, Skyline Icon Business Park, 86-92 Off A. K. Road,Marol Village, Andheri East,Mumbai, Maharashtra 400059',
'lat':'19.099910',
'lng':'72.915373',
'time':'NA',
'sex':'Unisex',
'place':'mall'
},
{
'address':'Garodia Shopping Centre,Ghatkopar East,Mumbai, Maharashtra 400077',
'lat':'19.074185',
'lng':'72.908169',
'time':'NA',
'sex':'Male',
'place':'mall'
},
{
'address':'EF Mall,Andheri,Rambaug, MHADA Colony 20, Powai,Mumbai, Maharashtra 400076',
'lat':'19.119056',
'lng':'72.901324',
'time':'NA',
'sex':'Male',
'place':'mall'
},
];
which also have dynamic variable details.['distance']
.Here getDistance()
is haversine formula.
var dist = getDistance(lt,lg,lat,lng);
details[i]['distance'] = dist;
All these values in list gets displayed in div for which I want to sort the list by distance.
for (i = 0; i < details.length; i++) {
var add= details[i].address;
var lat = details[i].lat;
var lng = details[i].lng;
var time = details[i].time;
var latLng = new google.maps.LatLng(lat, lng);
var Gender = details[i].sex;
var type = details[i].place;
var dist = getDistance(lt,lg,lat,lng);
details[i]['distance'] = dist;
document.getElementById('list').innerHTML +='<p><img src="'+type+'.png" style="float:left;margin-right:5;"><div id="address"> ' + add +'</div><br><img style="height:30;float:right;" src="'+Gender+'.png" title="+Gender+"><a class="review" href="#">See Reviews</a>'+'<img style="float:right;margin-top:6;" src="write.png" title="Write a Review"><div id="time">Timings:'+ time +'</div>'+details[i].distance +'km'+'</p><hr/>';
}
My implementation is as follows
details.sort(function(a,b) { return parseFloat(a.distance) - parseFloat(b.distance) } );
However this only sorts values by distance and displays the other values of lists as it is like address,type
(i.e it shows distance of some other address to some another address after sorting).The whole set of array must get sort without having wrong details.
Upvotes: 2
Views: 5488
Reputation: 7117
var objs = [
{ first_nom: 'Lazslo', last_nom: 'Jamf' },
{ first_nom: 'Pig', last_nom: 'Bodine' },
{ first_nom: 'Pirate', last_nom: 'Prentice' }
];
for(var i=0; i<objs.length; i++){
objs[i]['distance'] = objs[i]['last_nom'];
}
function compare(a,b) {
if (a.distance < b.distance)
return -1;
if (a.distance > b.distance)
return 1;
return 0;
}
objs.sort(compare);
alert(JSON.stringify(objs));
out put: [{"first_nom":"Pig","last_nom":"Bodine","distance":"Bodine"},{"first_nom":"Lazslo","last_nom":"Jamf","distance":"Jamf"},{"first_nom":"Pirate","last_nom":"Prentice","distance":"Prentice"}]
Upvotes: 3
Reputation: 680
It's a bit hard to answer your question since there is no distance property in your array as pointed out by birdspider. In any case, your method should work. You can test this in the console.
var details = [{'distance':'24.1','b':99},{'distance':'-12.5','b':100},{'distance':'35.6','b':101}]
details.sort(function(a,b) { return parseFloat(a.distance) - parseFloat(b.distance) } )
Now check the details variable again and you'll see:
[{'distance':'-12.5','b':100},{'distance':'24.1','b':99},{'distance':'35.6','b':101}]
Upvotes: 0