I believe the simplest command is
sort -t- -k3.1,3.4 -k2,2 file
This sorts on four characters of the third field and resolve ties by sorting on the second field. If ties are still not solved the order will be determined automatically by comparing all other fields starting from the first one.
Notice that all sorting is done alphabetically because numbers are positive integers so adding additional n
doesn't matter.
Test sample:
06-12-2016,12:00,gym,leg day
05-04-2018,12:09,gym,hands
09-08-2019,13:11,movies,horror
09-08-2019,13:09,movies,horror
09-08-2019,13:08,movies,horror
08-08-2019,13:09,movies,horror
05-04-2019,14:07,gym,hands
23-03-2018,16:47,dance class
25-04-2019,13:29,dance class
05-12-2017,14:45,songwriting
Sorted result:
06-12-2016,12:00,gym,leg day
05-12-2017,14:45,songwriting
23-03-2018,16:47,dance class
05-04-2018,12:09,gym,hands
05-04-2019,14:07,gym,hands
25-04-2019,13:29,dance class
08-08-2019,13:09,movies,horror
09-08-2019,13:08,movies,horror
09-08-2019,13:09,movies,horror
09-08-2019,13:11,movies,horror
YYYY-MM-DD
dates... – Kusalananda♦ Dec 13 '18 at 18:07sort -n -t\- -k 3.1 -k 2.1 -k 1.1 file
– Dougie Dec 13 '18 at 18:1206-12-2016,09:00,teatime
or is it06-12-2016,9:00,teatime
? – Jeff Schaller♦ Dec 13 '18 at 18:34