I have this script here
let myVar = [{time: "3:00 pm", name: "Landon"},{time: "5:20 pm", name: "Amanda"},{time: "4:00 pm", name: "Kid"}]
const sortedArray = myVar.sort((a,b) => new moment(a.time).format('hh:mm a') - new moment(b.time).format('hh:mm a'))
console.log(sortedArray)
The idea is that we sort the array by time. But its not working and Im not sure why.
Every google search leads me to answers like this: https://gist.github.com/onildoaguiar/6cf7dbf9e0f0b8684eb5b0977b40c5ad
But it does not work for me.
Your code passes a format at the wrong moment. You need to specify the input format, not the output format.
So change to:
let myVar = [{time: "3:00 pm", name: "Landon"},{time: "5:20 pm", name: "Amanda"},{time: "4:00 pm", name: "Kid"}]
const sortedArray = myVar.sort((a,b) => new moment(a.time, 'hh:mm a') - new moment(b.time, 'hh:mm a'))
console.log(sortedArray)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.3/moment.min.js" ></script>