利用数组的方法sort
代码如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2.7.14/dist/vue.js"></script>
</head>
<body>
<div id="app">
{{arr}}
{{listsort}}
</div>
</body>
<script>
new Vue({
el:'#app',
data:{
arr : [
{
id:1,
name:'张三',
price:100
},
{
id:2,
name:'李四',
price:300
},
{
id:3,
name:'王五',
price:200
}
]
},
computed: {
listsort() {
this.arr.sort(function (a,b){
return a.price - b.price //a-b 升序, b-a降序
})
},
},
})
</script>
</html>
运行结果:
![](https://img-blog.csdnimg.cn/img_convert/cada545803164ac4aa161737875b9bb8.png)