0

enter image description hereFollowing is dynamic array in TypeScript and I want to sort it by activation date in descending order.

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"},
]

I am sorting using below code but it's not giving me expected output.

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
          });

Any suggestion or input ?

3
  • did you check my answer? Commented Jan 19, 2018 at 20:23
  • Yes Arvind. It's looks same as what I given above. Although I tried your given code as well but no luck. I don't know what is the issue. Commented Jan 19, 2018 at 21:09
  • what you tried using my code can you edit the plunker in it and give me link so i can work on it and help you more Commented Jan 19, 2018 at 21:10

2 Answers 2

1

The callback to sort should return a number :

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
});

Note The date format you use is not officially supported and will only work in Chrome. I removed the - from the dates to convert the dates to a supported format.

Sign up to request clarification or add additional context in comments.

6 Comments

I don't why it's not taking effect. attached screenshot after applying above how it looks like. Don't know why it's not working.
Your solution is working fine in Chrome but it's not working in IE and FireFox. Any suggestion ?
Sorry not at the PC anymore.. I'll check tomorrow of it isn't too late..
@PushkarRathod The problem is not with the sorting it's with the date format, your date format is not officially supported: w3schools.com/js/js_date_formats.asp. Changed the answer to use a format supported across browsers
I can't change the date format because it's a requirement. The date format should be DD-MMM-YYYY only. Let me know what can be done to support in all browser.
|
0

Your comparison is wrong you should be having < instead of <

this.notificationList.sort((a, b)=> {
  let dateA = new Date(a['ActivationDate']);
  let dateB =  new Date(b['ActivationDate']);
  return dateA < dateB; // sort by descending
});

LIVE DEMO

7 Comments

If I use above mentioned code it throws me following error. Argument of type '(a: any, b: any) => boolean' is not assignable to parameter of type '(a: any, b: any) => number'. Type 'boolean' is not assignable to type 'number'.
Argument of type '(a: any, b: any) => boolean' is not assignable to parameter of type '(a: any, b: any) => number'. Type 'boolean' is not assignable to type 'number'.
@PushkarRathod can you update it to post?. are you available in teamviewer?
No. I can't do team viewer. But I can mail the whole object which I want to sort. Can you share me your id please ?
reach me out in fb @ aravind2109 we can work from there on
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.