I’m writing a bash script for some automation, and I can’t seem to find the best way of sorting an array that contains dates.
An example array:
dates=('180212', '110112', '040312')
The output should be as follows:
110112
180212
040312
I’ve tried to use sort but it just sorts it as if it were a integer.
Could anyone advise on the best method to use? It doesn’t necessarily have to sort either, I just need the oldest date from the array.
Thanks!
You could swap the year and the day digits, and use
sortto sort the resulting values. Then take the oldest value and undo the swap. You can usesedlike this to do the swapping:This swaps the first two with the last two digits.