Since you say all of these times are in PM, 12-hour notation is not zero-based (12, 1, 2, ... 11). It's probably easiest to convert these times to a zero-based notation, in which case 00:00 and 00:45 both come before 01:00.
To achieve this we can simply take the remainder of the hours if we divide it by 12.
const zeroBasedHour = hour % 12; // 0-11, instead of 12, 1-11
Let's create a function that does the conversion and adds the minutes in decimal form.
// 12-hour notation without am/pm.
// The initial 0 in 03:45 is optional.
function decimalTime(timeNotation) {
const [, hours, minutes] = timeNotation.match(/^(\d?\d):(\d\d)$/);
return parseInt(hours, 10) % 12
+ parseInt(minutes, 10) / 60;
}
This function would convert 12:00 to the decimal value 0. 12:45 is converted to the decimal value 0.75. And 01:00 is converted into 1. This makes sorting the array based on time alone very easy.
const arr = [
{ id: '5', Time: '01:00', status: 'grey' },
{ id: '7', Time: '12:00', status: 'grey' },
{ id: '8', Time: '12:45', status: 'green' },
];
arr.sort((a, b) => decimalTime(a.Time) - decimalTime(b.Time));
console.log(arr);
function decimalTime(timeNotation) {
const [, hours, minutes] = timeNotation.match(/^(\d?\d):(\d\d)$/);
return parseInt(hours, 10) % 12
+ parseInt(minutes, 10) / 60;
}
Note that the above assumes times are all either AM or PM, but not a mix of the two. You can very easily add AM/PM support by adding an additional 12 to final value if the provided time is PM. Do note that the function below no longer accepts a time without AM/PM.
// 12-hour notation with am/pm.
// The initial 0 in 03:45 is optional.
// The space between the time and am/pm is also optional.
// am/pm is case insensitive.
function decimalTime(timeNotation) {
const modifiers = { am: 0, pm: 12 };
const [, hours, minutes, modifier] =
timeNotation.match(/^(\d?\d):(\d\d) ?(am|pm)?$/i);
return parseInt(hours, 10) % 12
+ modifiers[modifier.toLowerCase()]
+ parseInt(minutes, 10) / 60;
}
"1:00" > "12:00"isn't what's in the object."01:00" > "12:00"is false.