<template>
  <div>
    <el-button @click="sort()">时间排序</el-button>
  </div>
</template>

 
<script>
export default {
  name: 'TimeSort',
  components: {},
  props: {},
  data() {
    return {
      timeData: [{
        id: '1',
        time:'2019-1-19 01:04:04'
      }, {
        id: '2',
        time:'2019-1-22 03:03:03'
      }, {
        id: '3',
        time:'2019-1-19 02:02:02'
      }, {
        id: '4',
        time:'2019-1-30 01:03:04'
      }]
    }
  },
  filters: {},
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  beforeDestroy() {},
  methods: {
    sort(){
      // this.timeData.sort(this.compare());
      this.timeData.sort(function(a, b) {
        let obj1 = a['time']
        let obj2 = b['time']
        const val1 = Math.floor(new Date(obj1).getTime() / 1000)
        const val2 = Math.floor(new Date(obj2).getTime() / 1000)
        return val1 - val2
        // return val2 - val1
      });
      console.log(this.timeData)
    },
    // compare(){
    //   return function(a, b) {
    //     let obj1 = a['time']
    //     let obj2 = b['time']
    //     const val1 = Math.floor(new Date(obj1).getTime() / 1000)
    //     const val2 = Math.floor(new Date(obj2).getTime() / 1000)
    //     return val1 - val2
    //     // return val2 - val1
    //   }
    // }
  }
}
</script>

Logo

前往低代码交流专区

更多推荐