0

I have the following response from a service:

const arrDate = [
  {
    "id":1,
    "birthDate":"2022-06-30T14:38:28.003"
  },
  {
    "id":2,
    "birthDate":"2022-06-30T14:36:48.50"
  },
  {
    "id":3,
    "birthDate":"2022-06-30T14:35:40.57"
  },
  {
    "id":4,
    "birthDate":"2022-06-30T13:09:58.451"
  },
  {
    "id":5,
    "birthDate":"2022-06-30T14:11:27.647"
  },
  {
    "id":6,
    "birthDate":"2022-06-30T14:55:41.41"
  },
  {
    "id":7,
    "birthDate":"2022-02-22T11:55:33.456"
  }
]

To sort it by date I do the following:

const sortDate = arrDate.sort(function(a, b) {
  return new Date(a.birthDate) > new Date(b.birthDate);
});

But the value of "sortDate" is not correct:

[
   {
      "birthDate":"2022-06-30T14:38:28.003",
      "id":1
   },
   {
      "birthDate":"2022-06-30T14:36:48.50",
      "id":2
   },
   {
      "birthDate":"2022-06-30T14:35:40.57",
      "id":3
   },
   {
      "birthDate":"2022-06-30T13:09:58.451",
      "id":4
   },
   {
      "birthDate":"2022-06-30T14:11:27.647",
      "id":5
   },
   {
      "birthDate":"2022-06-30T14:55:41.41",
      "id":6
   },
   {
      "id":7,
      "birthDate":"2022-02-22T11:55:33.456"
   }
]

Should be:

[
   {
      "birthDate":"2022-06-30T14:55:41.41",
      "id":6
   },
   {
      "birthDate":"2022-06-30T14:38:28.003",
      "id":1
   },
   {
      "birthDate":"2022-06-30T14:36:48.50",
      "id":2
   },
   {
      "birthDate":"2022-06-30T14:35:40.57",
      "id":3
   },
   {
      "birthDate":"2022-06-30T14:11:27.647",
      "id":5
   },
   {
      "birthDate":"2022-06-30T13:09:58.451",
      "id":4
   },
   {
      "id":7,
      "birthDate":"2022-02-22T11:55:33.456"
   }
]

How can i solve it?, thanks

5
  • stackoverflow.com/questions/5002848/… Commented Jul 1, 2022 at 7:20
  • 1
    change it to return new Date(a.birthDate) > new Date(b.birthDate) ? -1 : 1; Commented Jul 1, 2022 at 7:22
  • 1
    or real programmers do new Date(b.birthDate) - new Date(a.birthDate) Commented Jul 1, 2022 at 7:23
  • 3
    just take localeCompare, as they are strings and in ISO format. Commented Jul 1, 2022 at 7:31
  • @NinaScholz that's what real real programmers do :p Commented Jul 1, 2022 at 7:38

2 Answers 2

1

The problem is that the comparison function is supposed to return a positive number for if a should be after b, negative if b should be after a, and zero if they should keep the same order. Your function is returning true and false, JS converts true to 1 and false to zero. That means you are never telling sort to put b after a.

have the function return

return new Date(a.birthDate) > new Date(b.birthDate) ? -1 : 1;
// copied from a comment
Sign up to request clarification or add additional context in comments.

Comments

0

ascending

return new Date(a.birthDate) - new Date(b.birthDate);

descending

return new Date(b.birthDate) - new Date(a.birthDate);

Comments

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.