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.
@jonasholtkamp thank you for sharing the shortest (and to me most elegant) way!
@jonasholtkamp thank you for sharing the shortest (and to me most elegant) way ! (2)
@jonasholtkamp thank you for sharing the shortest (and to me most elegant) way! (3)
how can we do on this on react native
how can we do on this on react native
moment is supported by react native so the code does not change.
@jonasholtkamp thank you for sharing the shortest (and to me most elegant) way! (4)
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!
May I suggest
moment().diff
?diff
takes the usualmoment
input, e.g. a date-formatted string.