I have array INPUTFILES with n files
INPUTFILES=( file_0 ... files_n-1 )
And i need to sort them in the array order by first row in files.
Files look like this:
2012.09.20 17:10
2012.11.21 00:10
2012.12.22 15:10
2012.12.23 15:10
I have already function to compare 2 files:
IsSooner () {
ONEFIRST=$( head -1 "${1}" )
ONELAST=$( tail -1 "${1}" )
TWOFIRST=$( head -1 "${2}" )
TWOLAST=$( tail -1 "${2}" )
TIMEFORMAT='Y.%m.%d %H:%M:'
perl <<EOF
use strict;
use warnings;
use Time::Piece;
open STDERR, "> /dev/null";
my @dates1 = ("${ONEFIRST}","${ONELAST}");
my @range1 = map Time::Piece->strptime("\$_", "${TIMEFORMAT}"), @dates1;
my @dates2 = ("${TWOFIRST}","${TWOLAST}");
my @range2 = map Time::Piece->strptime("\$_", "${TIMEFORMAT}"), @dates2;
if ( \$range1[0] < \$range2[0] ) {
exit 0;
}
exit 1;
EOF
[ $? -eq 0 ] && {
return 0
}
return 1
}
Earlier will be first date in the file, the smaller index in the array will have.
Solution in BASH if preferable.
UPDATE
I don’t know format of date in advance. I just know it will be in strftime(3c) format.
In simple loop read first line of each file and save this information to hash, having first row data a hash key and the file name a hash value.
Sort the hash by the keys and print all values of sorted hash.
If you don’t want to print it, just store it back to the array, then do just
Good luck!
[UPDATE]
To follow the update in your question I suggest you to store values to hash using: