Reputation:
This is my sample xml
<markers>
<marker location_id="1" title="test 1" distance="0.0832"/>
<marker location_id="2" title="test 2" distance="3.1852"/>
<marker location_id="3" title="test 3" distance="4.3761"/>
<marker location_id="4" title="test 4" distance="3.3761"/>
</markers>
var entries = xml.documentElement.getElementsByTagName('marker');
How can I sort this entries by distance, ascending order ? I want to do it using Javascript.
Upvotes: 1
Views: 1309
Reputation: 337560
You can use the sort
method:
var $markers = $('.marker', xml);
$markers.find('marker').sort(function(a, b) {
return parseFloat($(a).attr('distance')) > parseFloat($(b).attr('distance'));
}).appendTo($markers);
This code assumes that the XML you have is stored in the xml
variable.
Upvotes: 2