There is a js object array var ary=[{id:1,name:"b"},{id:2,name:"b"}] The requirement is sorted according to the value of name or id, here is a charming function
Function definition:
The code copy is as follows:
function keysrt(key,desc) {
return function(a,b){
return desc ? ~~(a[key] < b[key]) : ~~(a[key] > b[key]);
}
}
use:
The code copy is as follows:
var ary=[{id:1,name:"b"},{id:2,name:"b"}];
ary.sort(keysrt('name',true));
ary.sort(keysrt('name',false));
ary.sort(keysrt('id',false));
The above is the entire content of this article. Friends who need it, come and study it carefully, haha.