I have an array of objects like so:
[
{name: 'John Smith', date: '10:30 AM'},
{name: 'Jo Smith', date: '8:30 AM'},
{name: 'Ela Smith', date: '1:00 PM'},
{name: 'Dave Smith', date: '12:30 PM'},
{name: 'Elton Smith', date: '10:30 PM'}
]
I'd like to sort these from earliest to latest using dayjs()
which is what we're using for the rest of our project so I have to stick with it.
At the moment I'm trying to use this function like so:
dayjs.extend(isSameOrBefore);
dayjs.extend(customParseFormat);
const sortTime = (users) => {
return shifts.sort((a, b) =>
dayjs(a.date, 'h:mm A').isSameOrBefore(dayjs(b.date, 'h:mm A')) ? 1 : -1
);
};
I'd like the array to be sorted like so:
[
{name: 'Jo Smith', date: '8:30 AM'},
{name: 'John Smith', date: '10:30 AM'},
{name: 'Elton Smith', date: '10:30 AM'},
{name: 'Dave Smith', date: '12:30 PM'},
{name: 'Ela Smith', date: '1:00 PM'}
]
However, for some strange reason, it's sorting like so:
[
{name: 'Jo Smith', date: '8:30 AM'},
{name: 'Ela Smith', date: '1:00 PM'},
{name: 'John Smith', date: '10:30 AM'},
{name: 'Dave Smith', date: '12:30 PM'},
{name: 'Elton Smith', date: '10:30 AM'},
]
Any ideas as to why and how I can fix this?