arrays - 如何使用 Vue.js 2 按日期对数组进行排序

标签 arrays sorting datetime vue.js

是否有按日期或格式化日期对数组进行排序的函数或方法?

var sortbydate = new Vue({
  el: '#sortbydate',
  data: {    
    items: [
      { codeName: 'Looper', date: '25.03.1980' },
      { codeName: 'Sweetze', date: '25.03.1981' },
      { codeName: 'Lycon', date: '01.08.1991' }
    ]
  }
})
<ul id="sortbydate">
  <li v-for="(item, index) in items" style="list-style:none">
    {{ index }} - {{ item.codeName }}
  </li>
</ul>

最佳答案

只需要这样做,所以我会写下最简单的解决方案:

...
computed: {
    sortedItems: function() {
        this.items.sort( ( a, b) => {
            return new Date(a.date) - new Date(b.date);
        });
        return this.items;
    }
}
...

或者如果你想要一个类轮

...
computed: {
  sortedItems: function() {
    return this.items.sort((a, b) => new Date(a.date) - new Date(b.date))
  }
}
...

关于arrays - 如何使用 Vue.js 2 按日期对数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42225073/

相关文章:

arrays - 如何在 GNUplot 中定义和访问数组?

sorting - 如何在Kotlin中使用sortedWith()?

python - datetime.timestamp 在 pandas apply 和 dataframe 选择中返回不同的值

mysql - 在 MySQL 中添加特定的分钟数

javascript - 将 DATETIME 字符串转换为 Javascript 或 jQuery 中的时间和日期

c++ - 不断收到此段错误 :11 error

javascript - JavaScript数组是如何存储在内存中的

java - ElasticSearch - 定义用于排序的自定义字母顺序

javascript - Rails hide_field 或 JavaScript 数组被发布为逗号分隔的字符串而不是数组

javascript - 当键不存在时排序 javascript 使用 localecompare