简体   繁体   English

用于排序日期列的自定义排序功能不起作用

[英]Customsort function for sorting date column not working

Can someone help me with my customSort function for primeng table column sorting?有人可以帮助我使用 customSort 函数进行 primeng 表列排序吗? I have a Date column in my table which gets data from array of strings which i formated to 'hh:mm a' and its not sorting as i want it to sort (instead of sorting like 1am, 2am, 3am etc its currently sorting like 1am,1pm,10am,10pm, ... etc) i came up with this customSort function but its still giving me same result and everything else in my table sorts just fine with this customSort except the date column.我的表中有一个 Date 列,它从我格式化为 'hh:mm a' 的字符串数组中获取数据,并且它没有按我希望的方式排序(而不是像凌晨 1 点、凌晨 2 点、凌晨 3 点等排序,它目前排序像上午 1 点、下午 1 点、上午 10 点、晚上 10 点,...等)我想出了这个 customSort 函数,但它仍然给我相同的结果,我的表中的所有其他内容都可以使用这个 customSort 排序,除了日期列。 Any help please?有什么帮助吗?

customSort(event: SortEvent) {
    event.data.sort((data1, data2) => {
      let value1 = data1[event.field];
      let value2 = data2[event.field];
      let result = null;
      if (value1 == null && value2 != null) {
        result = -1;
      } else if (value1 != null && value2 == null) {
        result = 1;
      } else if (value1 == null && value2 == null) {
        result = 0;
      } else if (typeof value1 === 'string' && typeof value2 === 'string') {
        const time1 = Date.parse('1970-01-01 ' + value1);
        const time2 = Date.parse('1970-01-01 ' + value2);
        console.log(value1);

        if (time1 < time2) {
          result = -1;
        } else if (time1 > time2) {
          result = 1;
        } else {
          result = 0;
        }
      } else {
        result = value1 < value2 ? -1 : value1 > value2 ? 1 : 0;
      }
      return event.order * result;
    });
  }

in html table i added this attribute在 html 表中我添加了这个属性

code...
...
 [customSort]="true"
(sortFunction)="customSort($event)"
...code

i have done some changes but still same sorting result我做了一些更改,但排序结果仍然相同

customSort(event: SortEvent) {
    event.data.sort((data1, data2) => {
      let value1 = data1[event.field];
      let value2 = data2[event.field];
      let result = null;
      if (value1 == null && value2 != null) {
        result = -1;
      } else if (value1 != null && value2 == null) {
        result = 1;
      } else if (value1 == null && value2 == null) {
        result = 0;
      } else {
        const time1 = new Date(`1970-01-01 ${value1.padStart(5, '0')}`);
        const time2 = new Date(`1970-01-01 ${value2.padStart(5, '0')}`);
        const timestamp1 = time1.getTime();
        const timestamp2 = time2.getTime();
        if (timestamp1 < timestamp2) {
          result = -1;
        } else if (timestamp1 > timestamp2) {
          result = 1;
        } else {
          result = 0;
        }
      }
      return event.order * result;
    });
  }

why not add a new property "minutes" like为什么不添加一个新的属性“分钟”,比如

data.forEach(x=>{
   const values=x.date?x.date.split(' '):null;
   const hourMinutes=values?values[0].split(":"):null;
   x.minutes=hourMinutes?(+hourMinutes[0])*60+(+hourMinutes[1]):-999
   if (values && values[1]=="pm")
       x.minutes+=12*60
})

Then, the only is sorted by minutes然后,only按分钟排序

<ng-container matColumnDef="date" >
    <th mat-header-cell *matHeaderCellDef mat-sort-header="minutes"> 
       Date
    </th>
    <td mat-cell *matCellDef="let element"> {{element.date}} </td>
</ng-container>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM