2

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.

Community
  • 1
  • 1
Dimple
  • 453
  • 9
  • 22

2 Answers2

3
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"}]

DEMO

ozil
  • 6,930
  • 9
  • 33
  • 56
  • @ Dimple click on demo and see the alert it is sorted – ozil May 13 '15 at 12:17
  • ok. But my problem is i want all the properties to be sorted like `address` `type` or `place` with distance. Because i am displaying them as a set. – Dimple May 13 '15 at 12:21
  • @ Dimple [Sorting an Array of Objects by two Properties](http://stackoverflow.com/questions/10153846/sorting-an-array-of-objects-by-two-properties) – ozil May 13 '15 at 12:26
0

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}]
Stephen Hartzell
  • 660
  • 1
  • 7
  • 17
  • I do have the distance property which is calculated dynamically using [haversine formula](http://stackoverflow.com/questions/27928/how-do-i-calculate-distance-between-two-latitude-longitude-points) – Dimple May 13 '15 at 11:47
  • Gotcha. I've actually the haversine formula myself several times. Does my example work out for you in console? (Just hit F-12 in chrome and firefox.) Perhaps the issue is how the sorted values are put on the front-end. I don't think the problem is in the JavaScript. – Stephen Hartzell May 13 '15 at 12:21