you can use date
command to convert those dates to unix timestamps and just sort them, and you can then convert them back to any format you want.
Example, converting date to timestamp and then back:
date -d "Mon Feb 20 09:22:25 2023" +%s
1676884945
date -d "@1676884945" +%c
Mon Feb 20 09:22:25 2023
-d
says to show date/time expressed in the string, if you use unix time you need to put @
before the time
+%s
says to show date in unix epoch time
+%c
says show date in locale setting time format
You can then put those in some loops and convert time to unix epoch, sort easily and convert sorted back to any time format you want.
If you have dates in datefile, this would convert them to unix timestamp and sort them.
while IFS= read -r line; do date -d "$line" +%s; done < datefile | sort -n
you can then convert those sorted one back to any format you want
for i in $(while IFS= read -r line; do date -d "$line" +%s; done < datefile | sort -n); do date -d "@$i" +%c; done
For the example you gave you would get something like this
for i in $(while IFS= read -r line; do date -d "$line" +%s; done < datefile | sort -n); do date -d "@$i" +%c; done
Mon Feb 20 09:22:25 2023
Mon Feb 20 17:42:13 2023
Tue Feb 28 16:02:40 2023
Thu Mar 2 05:55:07 2023
Note, when you use %c you will get output in the format with accordance your locale settings, so not necessary the format you want.
If you would want to be sure to get the format from your example you would need to use +'%a %b %d %T %Y'
for i in $(while IFS= read -r line; do date -d "$line" +%s; done < datefile | sort -n); do date -d "@$i" +'%a %b %d %T %Y'; done
Mon Feb 20 09:22:25 2023
Mon Feb 20 17:42:13 2023
Tue Feb 28 16:02:40 2023
Thu Mar 02 05:55:07 2023
%a
abbreviated three letter day
%b
abbreviated three letter month
%d
day of month
%T
time with leading zero
%Y
year
You can also do conversion to something like year, month, day, time in all numbers with no spaces like 20230220092225
with format '%Y%m%dH%M%S'
and use that format for sorting or comparing dates
date -d "Mon Feb 20 09:22:25 2023" +'%Y%m%d%H%M%S'
20230220092225
jq
hasstrptime()
andsort
/sort_by()
, so you should be able to do it there.jq
on the original JSON document.