Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save onildoaguiar/6cf7dbf9e0f0b8684eb5b0977b40c5ad to your computer and use it in GitHub Desktop.
Save onildoaguiar/6cf7dbf9e0f0b8684eb5b0977b40c5ad to your computer and use it in GitHub Desktop.
Sorting an array by date with Moment.js
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)
@nguyendt214
Copy link

Nice

@nguyendt214
Copy link

For sorter we can use with Lodash
Ex:

const sortedArray = _.orderBy(array, (o: any) => {
      return moment(o.date.format('YYYYMMDD');
    }, ['asc']);

(or 'desc')

@SijmenHuizenga
Copy link

The following also takes seconds and milliseconds into account and is a bit shorter.

let sortedArray = array.sort((a, b) => a.valueOf() - b.valueOf())

@ryanirilli
Copy link

I appreciate you people, thanks for this.

@Kif11
Copy link

Kif11 commented Sep 24, 2019

Same as @SijmenHuizenga suggested but a bit more readable since .unix() returns unix epoch.

let sortedArray = array.sort((a, b) => a.unix() - b.unix())

Where a and b are moment objects.

@jonasholtkamp
Copy link

jonasholtkamp commented Oct 21, 2019

May I suggest moment().diff?

const sortedArray = array.sort((a, b) => a.diff(b))

diff takes the usual moment input, e.g. a date-formatted string.

@hannojg
Copy link

hannojg commented Dec 13, 2019

@jonasholtkamp thank you for sharing the shortest (and to me most elegant) way!

@owlauch
Copy link

owlauch commented Jan 20, 2020

@jonasholtkamp thank you for sharing the shortest (and to me most elegant) way ! (2)

@SijmenHuizenga
Copy link

@jonasholtkamp thank you for sharing the shortest (and to me most elegant) way! (3)

@goelvaibhav105
Copy link

how can we do on this on react native

@bakoserge
Copy link

how can we do on this on react native

moment is supported by react native so the code does not change.

@Kraloz
Copy link

Kraloz commented Apr 9, 2020

@jonasholtkamp thank you for sharing the shortest (and to me most elegant) way! (4)

@alexkirillovtech
Copy link

alexkirillovtech commented Apr 23, 2020

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!

@JailsonSousa
Copy link

thanks

@devchia254
Copy link

Thanks for this post everyone!

@fedshu
Copy link

fedshu commented Jul 10, 2020

@jonasholtkamp thanks!

@danieldourado
Copy link

You guys rock!

@igorturturro
Copy link

Working smoothly!

@adityaTekale
Copy link

@jonasholtkamp thanks!

@simonescob
Copy link

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())

@matheop
Copy link

matheop commented Aug 14, 2021

simple and clear, thanks!

@jlhernando
Copy link

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 usual moment input, e.g. a date-formatted string.

@Fernando74lr
Copy link

Thanks a lot!

@leunglong0123
Copy link

Thaknks!

@Ba7er
Copy link

Ba7er commented Dec 21, 2021

Thanks.

@sabricalivar
Copy link

sabricalivar commented Mar 5, 2022

Thanks!!! <3 here I am after trying everything... loving you for this! (and dancing for victory of course!)

@shakib04
Copy link

shakib04 commented Nov 1, 2022

Thanks!!! <3 here I am after trying everything... loving you for this! (and dancing for victory of course!)

hahahaha

@tkud04
Copy link

tkud04 commented Feb 28, 2023

May I suggest moment().diff?

const sortedArray = array.sort((a, b) => a.diff(b))

diff takes the usual moment input, e.g. a date-formatted string.

God bless you chief!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment