0

I have a JavaScript multidimensional array:

var people = [
    { id: 1, name: "Hannah", birthdate: "04/16/2002" },
    { id: 2, name: "Sofia",  birthdate: "03/18/2012" },
    { id: 3, name: "Robert", birthdate: "12/22/1997" },
];

How can I sort them by their birthday date?

Michael M.
  • 10,486
  • 9
  • 18
  • 34
Dhenhice
  • 103
  • 5
  • https://stackoverflow.com/questions/46020052/javascript-sort-object-by-dd-mm-yyyy-property?noredirect=1&lq=1 helps? – cmgchess May 02 '23 at 06:31

1 Answers1

1

you sort a birthday date value like:

var people = [
    { id: 1, name: "Hannah", birthdate: "04/16/2002" },
    { id: 2, name: "Sofia",  birthdate: "03/18/2012" },
    { id: 3, name: "Robert", birthdate: "12/22/1997" },
];

people.sort(function(a, b) {
  var dateA = new Date(a.birthdate);
  var dateB = new Date(b.birthdate);
  return dateA - dateB;
});

console.log(people);
Michael M.
  • 10,486
  • 9
  • 18
  • 34