How to sort Date/Time column in angular 4 material sort?

前端 未结 5 719
借酒劲吻你
借酒劲吻你 2021-02-13 21:08

I\'m using angular material table and using matSort for sorting. But it\'s not sorting the dates/time column. It takes the datetime column values as strings.

How to sor

5条回答
  •  耶瑟儿~
    2021-02-13 21:41

    The easiest way to sort on a date in the Material Table is converting your date to a Unix timestamp in the sortingDateAccessor.

    this.dataSource.sortingDataAccessor = (item, property) => {
            switch (property) {
                case 'birthday':
                    return new Date(item.birthday).getTime()
    
                default:
                    return item[property]
            }
    }
    

提交回复
热议问题