Reputation: 295
I am trying to sort a multidimensional JSON array (called jsontest here) using JQuery using this code:
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);
original jsontest if alerted using javascript is : [{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"}]
But after sorting: it becomes(x1 is):
({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)"})
Why these extra charcters at the beginning and end? Is there some problem with my sorting function. I am using JQuery 1.6 undefined)"})
Upvotes: 1
Views: 169
Reputation: 207527
I am not sure why you are using a jQuery functions to do this when you can do it with normal array sort.
var foo = jsontest.sort(sortLastNameDesc);
All of that "extra" stuff is the fact it is now a jQuery object and not an array.
Upvotes: 2