I have a list of strings, each one contains time like this:
"03:00 AM", "12:30 PM", "16:15"
I need to sort them in order: put "am" times first and compare hours, not just first digits in a string.
This works for groovy, with all cases you gave:
List dates = [ "16:15", "12:30 PM", "07:00", "03:00 AM" ]
dates.sort { d ->
[ 'h:mm a', 'H:mm' ].findResult { fmt ->
try {
Date.parse( fmt, d ).time
} catch( e ) {}
}
}
It basically tries with AM/PM and then tries without it if that fails
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With