我正在尝试使用JQuery对多维JSON数组(此处称为jsontest)进行排序,使用以下代码:
jQuery.fn.sort = function() {
return this.pushStack( [].sort.apply( this, arguments ), []);
};
function sortLastName(a,b){
if (a.arrtime1 == b.arrtime1){
return 0;
}
return a.arrtime1 > b.arrtime1 ? 1 : -1;
};
function sortLastNameDesc(a,b){
return sortLastName(a,b) * -1;
};
x1=$(jsontest).sort(sortLastNameDesc);
如果使用javascript发出警报,则原始jsontest为:[{f_name:"john", arrtime1:"10", sequence:"0", title:"president", url:"google.com", color:"333333"}, {f_name:"michael", arrtime1:"11", sequence:"0", title:"general manager", url:"google.com", color:"333333"}]
但排序后:变为(x1是):
({0:#1={f_name:"michael", arrtime1:"11", sequence:"0", title:"general manager", url:"google.com", color:"333333"}, 1:#2={f_name:"john", arrtime1:"10", sequence:"0", title:"president", url:"google.com", color:"333333"}, length:2, prevObject:{0:#1#, 1:#2#, length:2}, context:(void 0), selector:".(undefined)"})
为什么开头和结尾都有这些额外的字符?我的排序功能有问题吗。我使用的是JQuery 1.6 undefined)"})
我不知道为什么要使用jQuery函数来实现这一点,而你可以使用普通的数组排序。
var foo = jsontest.sort(sortLastNameDesc);
所有这些"额外"的东西都是,它现在是一个jQuery对象,而不是一个数组。