JSFiddle - React, Tailwind, and code Playground
by Wendell Christian
JavaScript
const valores =
[
{
"id": "1",
"name": "Valor 1",
"date": "2021-11-01 00:00:00"
},
{
"id": "2",
"name": "Valor 2",
"date": "2021-11-09 00:00:00"
},
{
"id": "3",
"name": "Valor 3",
"date": null
},
{
"id": "4",
"name": "Valor 4",
"date": "2021-11-03 00:00:00"
},
{
"id": "5",
"name": "Valor 5",
"date": null
}
];
function sortByDate(a, b) {
if (a.date > b.date) return -1;
if (a.date < b.date) return 1;
if (a.date === null || b.date === null) return 1;
return 0;
}
console.log(sortByDate(valores));