来源:【技术胖】大佬,专注前端的大佬,博客地址:https://www.jspang.com
1、排序数字
items:[20,23,18,65,32,19,5,56,41]
computed:{
sortItems:function(){
return this.items.sort(sortNumber);
}
}
function sortNumber(a,b){
return a-b
}
2、排序数组
students:[
{name:'jspang',age:32},
{name:'Panda',age:30},
{name:'PanPaN',age:21},
{name:'King',age:45}
]
computed:{
sortStudent:function(){
return sortByKey(this.students,'age');
}
}
//数组对象方法排序:
function sortByKey(array,key){
return array.sort(function(a,b){
var x=a[key];
var y=b[key];
return ((x<y)?-1:((x>y)?1:0));
});
}