function sortByKey(arr, key){
return arr.sort(function(a,b){
let x = a[key];
let y = b[key];
return ((x<y)?-1:((x>y)?1:0))
})
}
let cats = [
{name: 'Arya' ,age: 10},
{name: 'Winne' ,age: 5}
];
sortByKey(cats,'age')
image.png
网友评论