开发者

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;
      }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜