sorting - Javascript: sort multidimensional array -
after creating multi-dim array this, how sort it?
assuming 'markers' defined:
var location = []; (var = 0; < markers.length; i++) { location[i] = {}; location[i]["distance"] = "5"; location[i]["name"] = "foo"; location[i]["detail"] = "something"; }
for above example, need sort 'distance'. i've seen other questions on sorting arrays , multi-dim arrays, none seem work this.
location.sort(function(a,b) { // assuming distance valid integer return parseint(a.distance,10) - parseint(b.distance,10); });
javascript's array.sort
method has optional parameter, function reference custom compare. return values >0
meaning b
first, 0
meaning a
, b
equal, , <0
meaning a
first.
Comments
Post a Comment