米娜桑~好久不见啦 今天做个简单的排序总结之数组对象排序
代码贴出来~
这里的arr要根据自己的数组名进行修改,code要根据自己数组中对象的属性名进行调整,然后按照这个数组名进行排序
created() {
// 初始化下拉框 搭配类型
this.$axios.run(***, (res) => {
if (res.code === 0) {
this.arr= res.data;
// 对象排序
this.arr.sort(this.compare('code'));
this.selectInit(res.data);
}
});
this.onInit();
},
methods: {
// 对象排序
compare(property) {
return function(a, b) {
let value1 = a[property];
let value2 = b[property];
return value1 - value2;
};
},
呜啦啦啦~~~下班!