0

我有一个 JS 对象,带有另一个 JS 对象的内部数组。内部onw就像

function outerObj()
{
    this.items = new Array();
    this.clear = function () {
        this.items = new Array();
    }
    function __item(v1, v2, v3, v4) {
        this.Val_1 = v1;
        this.Val_2 = v2;
        this.Val_3 = v3;
        this.Val_4 = v4;
    }
    this.add = function (vA, vB, vC, vD) {
        this.items.push( new __item(vA, vB, vC, vD) );
    }
    ...
}

该数组通过 SPServicesgetListItems()调用加载,使用.each( . . . )生成的 XML。结果是有序的(<OrderBy>),但结果是常见的刺激:

1
10
11
2
21
3
42A
42B

我无法摆脱包含字母(这是现实世界项目的名称),而我希望排序是

1
2
3
10
11
21
42A
42B

建议?

4

2 回答 2

1

只需使用 javascript array.sort()

代码如下所示:

this.items.sort()

这是一个链接:Javascript 排序参考

于 2013-07-23T19:05:55.943 回答
1

实际上,以利沙,应该是这样的。

    items.sort(function (v1, v2) { 
        /* algorythm here */ 
    }); 

算法是我可以添加智能的地方,它知道数组包含对象而不是值,并且我可以真正检查我正在排序的属性。

事实上,我可以编写一个外部函数,并从匿名函数中调用它。

谢谢以利沙;这让我走上了正轨。

我在这里回答,以便我可以使用格式化答案的能力。但以利沙应该得到这个功劳。

顺便说一句,该函数需要返回一个负数、零或一个正数。返回的符号表示传入的两个参数之间的排序顺序。

于 2013-07-23T20:21:01.513 回答