Looks to me like you want something like this
Schwartzian Transform:
#open your files as before, then
print FOO map { $_->[1] }
sort { $a->[0] <=> $b->[0] }
map {my @F=split; [substr($F[2],1), $_]}
<D>;
#close your files as before
Reading from bottom to top, we construct an array of arrays, where the first element of each element is the sort-field, and the second is the print-field. Then we sort by comparing the sort fields, then we return only the print-fields to print.
Updated to work on the data provided.
Caution: Contents may have been coded under pressure.