Just use javascript array.sort()
code would look like this:
this.items.sort()
Heres a link: Javascript sort reference
题
I have a JS object, with an internal array of another JS object. The internal onw is like
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) );
}
...
}
The array is loaded through an SPServices getListItems()
call, using .each( . . . )
on the resultant XML. The results are ordered (<OrderBy>
), but the results are the common irritation:
1
10
11
2
21
3
42A
42B
I cannot get away from the inclusion of letters (this is the name of real-world items), while I want the sort to be
1
2
3
10
11
21
42A
42B
Suggestions?
解决方案
Just use javascript array.sort()
code would look like this:
this.items.sort()
Heres a link: Javascript sort reference
其他提示
Actually, Elisha, it would look like this.
items.sort(function (v1, v2) {
/* algorythm here */
});
The algorythm is where I can add the intelligence that is aware of the fact that the array is holding objects, not values, and where i can really examine the property upon which i am sorting.
In fact, I can write an external function, and call it from the anonymous function.
Thanks Elisha; this got me on track.
I am answering here, so that I can use the ability to format the answer. But Elisha should get the credit on this one.
Incidentally, the function needs to return a negative number, zero, or a positive number. The sign of what is returned indicates the sorting order between the two arguments passed in.