Hello, I have a requirement to sort elements in an array of hashes in perl and need your help for it. Given below is the code I have for it:
@List = (); $List[0]->{'time'} = "00:34:20"; $List[0]->{'date'} = "2011-09-13 00:34:20"; $List[0]->{'file'} = "abc.txt"; $List[0]->{'note'} = "dummy1"; $List[1]->{'time'} = "00:37:12"; $List[1]->{'date'} = "2011-09-13 00:37:12"; $List[1]->{'file'} = "abc1.txt"; $List[1]->{'note'} = "dummy2"; $List[2]->{'time'} = "00:33:00"; $List[2]->{'date'} = "2011-09-13 00:33:00"; $List[2]->{'file'} = "abc2.txt"; $List[2]->{'note'} = "dummy3"; my @sorted = sort { $a->{'date'} <=> $b->{'date'} } @List;
I need to sort the hashes by 'date' so when the array is printed, the files are listed by creation time, with the most recent being the first. But the entries do not get sorted. Any idea how to sort the hash entries in the array? Thanks!
In reply to Sorting an array of hashes by madhurjf
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |