asp.net

asp.net,c#
在javascript中对一个对象数组进行排序

function sort(photoes) {
    var temp, j;
    for (var i = 1; i < photoes.length; i++) {
        if (compare(photoes[i], photoes[i - 1]) == -1) {
            temp = photoes[i];
            j = i - 1;
            do {
                photoes[j + 1] = photoes[j];
                j--;
            }
            while (j > -1 && compare(temp, photoes[j]) == -1);
            photoes[j + 1] = temp;
        } //endif
    }
}

function compare(p1, p2) {
    var n1 = p1.src;
    n1 = n1.substr(n1.lastIndexOf('/') + 1);
    var n2 = p2.src;
    n2 = n2.substr(n2.lastIndexOf('/') + 1);
    if (n1 < n2) return -1;
    if (n1 == n2) return 0;
    else return 1;
}

 

BTW:这个例子中photos是一个html image element的数组.
 

posted on 2011-02-17 17:11  灵魂边缘  阅读(193)  评论(0编辑  收藏  举报