Javascript sort
Not sure what I am doing wrong. I am trying to sort the results.
function makeSidebar() {
v开发者_如何学运维ar html = "";
for (var i=0; i<gmarkers.length; i++) {
if (!gmarkers[i].isHidden()) {
html += '<a href="javascript:myclick(' + i + ')">' + gmarkers[i].myteam + '<\/a><br>';
}
}
document.getElementById("side_bar").innerHTML = html;
}
I have tried html.sort(); and a few different ideas.
Thanks
html
in your code is a string and strings don't have a sort function. Arrays however do, so you should sort the teams first, then display them:
function compare(a, b) {
if (a.myteam < b.myteam) return -1;
if (a.myteam > b.myteam) return 1;
return 0;
}
gmarkers.sort(compare);
function makeSidebar() {
var html = "";
for (var i=0; i<gmarkers.length; i++) {
if (!gmarkers[i].isHidden()) {
html += '<a href="javascript:myclick(' + i + ')">' + gmarkers[i].myteam + '<\/a><br>';
}
}
document.getElementById("side_bar").innerHTML = html;
}
精彩评论