Sort Array By Date In Javascript
Solution 1:
You could check if the property exists has the wanted format and then sort the date by string descending and if one has no valid format, take the delta of the boolean values.
functioncheckDate(string) {
returntypeof string === 'string'
&& /\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/.test(string);
}
var array = [{ date: "2019-06-15 14:57:13", user: "john" }, { date: "2019-06-15 05:48:01", user: "mike" }, { date: "bad-date-format", user: "donna" }, { date: "2019-06-08 10:45:09", user: "Ismil" }, { date: "", user: "Daniel17" }];
array.sort((a, b) => {
var aC = checkDate(a.date),
bC = checkDate(b.date);
return aC && bC
? b.date.localeCompare(a.date)
: bC - aC;
});
console.log(array);
.as-console-wrapper { max-height: 100%!important; top: 0; }
Solution 2:
You don't have to check if the string is a valid date.
Here's a working code:
const data = [{
"date": "2019-06-15 14:57:13",
"user": "john"
},
{
"date": "2019-06-15 05:48:01",
"user": "mike"
},
{
"date": "bad-date-format",
"user": "donna"
},
{
"date": "2019-06-08 10:45:09",
"user": "Ismil"
},
{
"date": "",
"user": "Daniel17"
}
];
const elements = data.sort((a, b) => (newDate(b.date).getTime() || -Infinity) - (newDate(a.date).getTime() || -Infinity));
console.log(elements);
The trick behind the above code is that new Date()
will give an Invalid Date
object if you pass an invalid date string to it, which will return NaN
if you executed its getTime()
method.
Now because you want all Invalid Date
s to be at the bottom, then your sorting function should treat these Invalid Date
s as the lowest rated in your array, and that's what -Infinite
means (the lowest number. If you add any number to it will lead to -Infinite
).
I assume that it doesn't matter how Invalid Date
s are sorted at the bottom of your array.
Solution 3:
As per OPs comment
order by date descending and the ones with no corrupt data should go at the beginning of the array
let data = [{
date: newDate('2019-01-03T00:00:00.000Z')
},{
date: newDate('2020-01-03T00:00:00.000Z')
},{
date: newDate('2018-01-03T00:00:00.000Z')
}, {}]
functionsafeGetTime(obj) {
if (obj && obj.date && obj.date.getTime) {
return obj.date.getTime();
}
returnNumber.MAX_SAFE_INTEGER; // replace with `return 0` to put invalid data at end of array
}
data.sort(function(a, b) {
returnsafeGetTime(b) - safeGetTime(a)
});
console.log(data);
Solution 4:
You can use Date.parse
to make sure you have a valid date and when you do for both of your parameters sort them. Otherwise sort the one with the valid date "higher":
let data = [{ "date": "2019-06-15 14:57:13", "user": "john" }, { "date": "2019-06-15 05:48:01", "user": "mike" }, { "date": "bad-date-format", "user": "donna" }, { "date": "2019-06-08 10:45:09", "user": "Ismil" }, { "date": "", "user": "Daniel17" } ]
let result = data.sort((a,b) =>Date.parse(a.date) && Date.parse(b.date)
? newDate(b.date).getTime() - newDate(a.date).getTime()
: Date.parse(a.date) ? -1 : 0
)
console.log(result)
Solution 5:
data.sort(function(a, b) {
if (typeof a.date !== 'undefined' && typeof b.date !== 'undefined') {
return a.date.getTime() - b.date.getTime()
}
return0
});
Working simple example
data = [{date:"100"},{date:null},{date:"1"}, {nodate:"10"}];
data.sort(function(a, b) {
if (typeof a.date !== 'undefined' && typeof b.date !== 'undefined') {
return a.date - b.date
}
return0
});
console.log(data)
Post a Comment for "Sort Array By Date In Javascript"