0

I am trying to sort the array based on how recent the date is. I would like the array to be sorted from newest to oldest date based on minutes. But the sorting function seems to be doing nothing.

// array
const arr = [{
    date_created: "2022-11-11T20:41:37.430Z",
    id: "1",
    title: "Call of Duty 2003",
    price: "199",
  },
  {
    date_created: "2022-11-11T20:46:15.567Z",
    id: "2",
    title: "Call of Duty: United Offensive (2004)",
    price: "200",
  },

  {
    date_created: "2022-11-11T20:30:38.841Z",
    id: "3",
    title: "Call of Duty: Finest Hour (2004)",
    price: "223",
  },
];

// sorting function
const array = arr.sort((a, b) =>
  moment(a.date_created).diff(moment(b.date_created), "minutes")
);

console.log(arr)
console.log(array)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
John low
  • 1
  • 3

0 Answers0