vue的默认排序方式
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  </head>
  <body>
    <div >
      <template v-for="user in userInfo">
        <li>姓名:{{user}}</li>
      </template>
    </div>
    <script>
        var v=new Vue({
            el:"#app",
            data:{
                userInfo:['zhangsan','aisi','wangwu']
            }
        });
       v.userInfo.sort();
    </script>
  </body>
</html>
 
改写排序方式的示例
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  </head>
  <body>
    <div >
      <template v-for="user in userInfoSorted">
        <li>姓名:{{user.name}}</li>
      </template>
    </div>
    <script>
      var v = new Vue({
        el: "#app",
        data: {
          userInfo: [
            { name: "zhangsan" },
            { name: "aisi" },
            { name: "wangwu" },
          ],
        },
        computed: {
          userInfoSortedfunction () {
// a,b参数代表的是user 按照 长度降序排序
            return this.userInfo.sort(function (a, b) {
              return -(a.name.length - b.name.length);
            });
          },
        },
      });
    </script>
  </body>
</html>

相关文章:

  • 2022-12-23
  • 2021-09-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-14
  • 2021-04-18
猜你喜欢
  • 2022-12-23
  • 2021-09-30
  • 2022-12-23
  • 2022-12-23
  • 2021-06-11
  • 2022-12-23
  • 2021-05-18
相关资源
相似解决方案