nikmit has asked for the wisdom of the Perl Monks concerning the following question:

This gets me tangled up, any suggestions welcome. A way of getting my head round it so can figure it out myself in the future would be most useful :)

I have an array built like so:

push(@array, { entry => [$a, $b, $c] });

I want to sort @array by the $b value, which happens to be a time stamp.

I know I could build that array directly as an array of array references but it makes some TemplateToolkit code which uses the data later much harder to read, and could break it too.

Replies are listed 'Best First'.
Re: Sorting an array of hash references of array references
by BillKSmith (Monsignor) on Aug 17, 2016 at 13:21 UTC

    It is a bad idea to use "$a" and "$b" as variable names, especially in a program that does sorting.

    Refer to the FAQ "How do I sort an array by (anything)?" for an alternate method. (Your approach is simpler and for small data sets it probably does not matter which you use.)

    Bill
Re: Sorting an array of hash references of array references
by nikmit (Sexton) on Aug 17, 2016 at 09:52 UTC

    OK, found http://www.perlmonks.org/?node_id=599204 and came up with this:

    my @sorted = sort { $a->{entry}->[1] cmp $b->{entry}->[1] } @array;

    Seems to do the trick.