1. 程式人生 > 其它 >ant-design-vue table對日期資料排序

ant-design-vue table對日期資料排序

技術標籤:jsant-design-vuevue.jsant-design

ant-design-vue table對日期資料排序


一、js對日期資料排序

使用sort()方法

// 
 let data = [
       {
           id:'1',
           time:'2020-01-01 23:53:23'
       },
       {
            id:'2',
             time:'2021-01-08 8:53:10'
       },{
            id:'3',
             time:'2019-12-31 23:59:23'
       },{
            id:'4',
            time:'2020-01-01 10:53:23'
       }
   ]
   data.sort(function(a,b){
   //降序
       return a.time < b.time ? 1 : -1
       //升序  return a.time > b.time ? 1 : -1
   });

得到結果 降序

0: {id: "2", time: "2021-01-08 8:53:10"}
1: {id: "1", time: "2020-01-01 23:53:23"}
2: {id: "4", time: "2020-01-01 10:53:23"}
3: {id: "3", time: "2019-12-31 23:59:23"}


二、在 a-table 中使用

設定表格 columns

const columns = [ {
    title:
'時間', dataIndex: 'time', defaultSortOrder: 'descend', // 預設上到下為由大到小的順序 sorter: (a, b) => { return a.time> b.time? 1 : -1 }, },]

在這裡插入圖片描述