madhurjf has asked for the wisdom of the Perl Monks concerning the following question:
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!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Sorting an array of hashes
by Corion (Patriarch) on Sep 22, 2011 at 07:14 UTC | |
|
Re: Sorting an array of hashes
by Kc12349 (Monk) on Sep 22, 2011 at 17:58 UTC | |
by madhurjf (Novice) on Sep 23, 2011 at 05:47 UTC | |
|
Re: Sorting an array of hashes
by Anonymous Monk on Sep 22, 2011 at 15:26 UTC | |
|
Re: Sorting an array of hashes
by petdance (Parson) on Sep 22, 2011 at 14:50 UTC |