Sorting 在TypeScript中对密钥对值数组进行排序

Sorting 在TypeScript中对密钥对值数组进行排序,sorting,typescript,dynamic-arrays,Sorting,Typescript,Dynamic Arrays,下面是TypeScript中的动态数组,我想按激活日期降序排序 notificationList = []; notificationList = [ {"Id:11", "ActivationDate":"29-Jan-2018"}, {"Id:21", "ActivationDate":"22-Jan-2018"}, {"Id:8", "ActivationDate":"01-Feb-2018"}, {"Id:10", "ActivationDate":"25-Jan-2018"}, {"

下面是TypeScript中的动态数组,我想按激活日期降序排序

notificationList = [];

notificationList = [ {"Id:11", "ActivationDate":"29-Jan-2018"},
{"Id:21", "ActivationDate":"22-Jan-2018"},
{"Id:8", "ActivationDate":"01-Feb-2018"},
{"Id:10", "ActivationDate":"25-Jan-2018"},
{"Id:12", "ActivationDate":"24-Jan-2018"},
{"Id:05", "ActivationDate":"28-Jan-2018"},
{"Id:04", "ActivationDate":"24-Jan-2018"},
]
我使用下面的代码进行排序,但它没有给我预期的输出

this.notificationList = this.notificationList.sort(function(a, b): any {
              const dateA = new Date(a['ActivationDate']);
              const dateB = new Date(b['ActivationDate']);
              console.log('dateA -' + dateA);
              console.log('dateB -' + dateB);
              console.log(dateB > dateA);
              return dateB > dateA; //sort by date decending
          });

任何建议或输入?

要排序的回调应:

let notificationList=[
{“Id”:11,“激活日期”:“2018年1月29日”},
{“Id”:21,“激活日期”:“2018年1月22日”},
{“Id”:8,“激活日期”:“2018年2月1日”},
{“Id”:10,“激活日期”:“2018年1月25日”},
{“Id”:12,“激活日期”:“2018年1月24日”},
{“Id”:5,“激活日期”:“2018年1月28日”},
{“Id”:4,“激活日期”:“2018年1月24日”},
];
notificationList=notificationList.sort(函数(a,b):任意{
const dateA=新日期(a['ActivationDate']);
const dateB=新日期(b['ActivationDate']);
return dateB>dateA?1:dateB

注意您使用的日期格式是并且仅在Chrome中可用。我从日期中删除了
-
,以将日期转换为支持的格式。

您的比较是错误的,您应该有
我不知道为什么它没有生效。附加的屏幕截图后,应用上面它看起来如何。不知道为什么不起作用。你的解决方案在Chrome中运行良好,但在IE和FireFox中不起作用。有什么建议吗?对不起,我不在电脑前了。。明天我会检查的,不会太晚。@PushkarRathod问题不在于排序,而在于日期格式,您的日期格式不受官方支持:。已将答案更改为使用浏览器支持的格式。我无法更改日期格式,因为这是一项要求。日期格式应仅为DD-MMM-YYYY。让我知道可以在所有浏览器中支持什么。您检查了我的答案吗?是的,Arvind。它看起来和我上面说的一样。虽然我也尝试了你给出的代码,但没有运气。我不知道是什么问题。你尝试使用我的代码时,你能在其中编辑plunker并给我链接,这样我就可以处理它并帮助你更多如果我使用上述代码,它会引发以下错误。类型为“(a:any,b:any)=>boolean”的参数不能分配给类型为“(a:any,b:any)=>number”的参数。类型“boolean”不可分配给类型“number”。类型为“(a:any,b:any)=>boolean”的参数不可分配给类型为“(a:any,b:any)=>number”的参数。类型“boolean”不能分配给类型“number”。@PushkarRathod您能将其更新为post吗?。您在teamviewer中可用吗?不,我不能使用teamviewer。但是我可以邮寄我想要排序的整个对象。你能告诉我你的身份证吗?请在fb@aravind2109联系我,我们可以从那里开始工作
let notificationList = [
    { "Id": 11, "ActivationDate": "29 Jan 2018" },
    { "Id": 21, "ActivationDate": "22 Jan 2018" },
    { "Id": 8, "ActivationDate": "01 Feb 2018" },
    { "Id": 10, "ActivationDate": "25 Jan 2018" },
    { "Id": 12, "ActivationDate": "24 Jan 2018" },
    { "Id": 5, "ActivationDate": "28 Jan 2018" },
    { "Id": 4, "ActivationDate": "24 Jan 2018" },
];

notificationList = notificationList.sort(function (a, b): any {
    const dateA = new Date(a['ActivationDate']);
    const dateB = new Date(b['ActivationDate']);
    return dateB > dateA ? 1 : dateB < dateA ? -1 : 0; //sort by date decending
});
this.notificationList.sort((a, b)=> {
  let dateA = new Date(a['ActivationDate']);
  let dateB =  new Date(b['ActivationDate']);
  return dateA < dateB; // sort by descending
});