在我们工作中经常会遇到数组排序这样的东西,遇到了顺便就记录下来
<div v-for="student,index in sortStudent" :key="index"> {{index+1}} {{ student.name }}--{{student.age}} </div>
vue代码
student: [{ name: 'Sugar', age: '23' }, { name: 'Yang', age: '20' }, { name: 'xiucheng', age: '18' } ] }
js
computed:{ sortStudent:function(){ return sortByKey(this.student,'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) }) }