const Moment = require('moment')
const array = [{date:"2018-05-11"},{date:"2018-05-12"},{date:"2018-05-10"}]
const sortedArray = array.sort((a,b) => new Moment(a.date).format('YYYYMMDD') - new Moment(b.date).format('YYYYMMDD'))
console.log(sortedArray)
-
-
Save onildoaguiar/6cf7dbf9e0f0b8684eb5b0977b40c5ad to your computer and use it in GitHub Desktop.
The following also takes seconds and milliseconds into account and is a bit shorter.
let sortedArray = array.sort((a, b) => a.valueOf() - b.valueOf())
@SijmenHuizenga Man, thanks!
thanks
Thanks for this post everyone!
@jonasholtkamp thanks!
You guys rock!
Working smoothly!
@jonasholtkamp thanks!
The following also takes seconds and milliseconds into account and is a bit shorter.
let sortedArray = array.sort((a, b) => a.valueOf() - b.valueOf())
Thanks for this aport. Using moment in the code will be so:
let sortedArray = array.sort((a,b) => moment(a).valueOf() - moment(b).valueOf())
simple and clear, thanks!
This worked for me really nicely! thanks @jonasholtkamp
May I suggest
moment().diff
?const sortedArray = array.sort((a, b) => a.diff(b))
diff
takes the usualmoment
input, e.g. a date-formatted string.
Thanks a lot!
Thaknks!
Thanks.
Thanks!!! <3 here I am after trying everything... loving you for this! (and dancing for victory of course!)
Thanks!!! <3 here I am after trying everything... loving you for this! (and dancing for victory of course!)
hahahaha
May I suggest
moment().diff
?const sortedArray = array.sort((a, b) => a.diff(b))
diff
takes the usualmoment
input, e.g. a date-formatted string.
God bless you chief!
@jonasholtkamp thank you for sharing the shortest (and to me most elegant) way! (4)