/    Sign up×
Community /Pin to ProfileBookmark

I use the following JS code for a map using the Google Maps API. I want to alphabetically sort the results. Right now, they just come out in order that they are in the XML file.

[code]

var request = GXmlHttp.create();
request.open(“GET”, “locations.xml”, true);
request.onreadystatechange = function() {
if (request.readyState == 4) {
var xmlDoc = request.responseXML;
// obtain the array of markers and loop through it
var markers = xmlDoc.documentElement.getElementsByTagName(“marker”);

for (var i = 0; i < markers.length; i++) {
// obtain the attribues of each marker
var lat = parseFloat(markers[i].getAttribute(“lat”));
var lng = parseFloat(markers[i].getAttribute(“lng”));
var point = new GLatLng(lat,lng);
var label = markers[i].getAttribute(“label”);
var html = “<span class=’windowTitle’>” + label + “</span><p class=’window’>” + GXml.value(markers[i].getElementsByTagName(“infowindow”)[0]) + “</p>”;
// create the marker
var marker = createMarker(point,label,html);
map.addOverlay(marker);
}
// put the assembled sidebar_html contents into the sidebar div
document.getElementById(“sidebar”).innerHTML = sidebar_html;
}
}
request.send(null);
}

[/code]

The for loop just iterates though the file in order. How would I go about alphabetizing the results?

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceJun 19.2006 — The process building the XML file would need to do that. Otherwise, to the best of my knowledge, your only other choice is to use JavaScript to extract and load the XML data into a JavaScript 2-dimensional array and use the array's built-in [B]sort()[/B] method.
×

Success!

Help @ibeinboston spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.23,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...