const arr = [{name:'小李',value:'10'},{name:'小王',value:'5'},{name:'小张',value:'11'}]// 正序(从小到大)function compare1 (key) {return function (arr1,arr2){return arr1[key] - arr2[key]}}// 倒序(从大到小)function compare2 (key) {return function (arr1,arr2){return arr2[key] - arr1[key]}}//根据value值来排正序arr.sort(compare1('value'));//输出[{name:'小王',value:'5'},{name:'小李',value:'10'},{name:'小张',value:'11'}]//根据value值来排倒序arr.sort(compare2('value'));//输出[{name:'小张',value:'11'},{name:'小李',value:'10'},{name:'小王',value:'5'}]
1200字范文,内容丰富有趣,写作的好帮手!