Sort date in Javascript based on multiple values, some of which are optional

Javascript
19/04/2024

For this blog, I wanted to be able my items coming from the API by date. But some have an optional 'displayDate' and some don't. If they don't, I would like to use the CreateDate-property of the Umbraco-node.

 

Here's how I did this:

I created a helper-function, so I can reuse it again.

I've placed this file in my utilities/helpers.js-file and now I can reference it in Vue by using:

If you want to sort by date ascending, just switch the two brackets (> to >) and vice versa