in reply to sorting array of hashes using multiple keys conditionally

Not two sorts. One sort with a properly crafted compare function.

@a = sort { $a->{key1} <=> $b->{key1} || $a->{key2} <=> $b->{key2} } @a;

I've been reading up on Schwartzian Transforms, which I'm still not sure are applicable here.

It's not. It's used to minimize the cost of expensive compares (e.g. if I/O is done) when sorting long lists.

Replies are listed 'Best First'.
Re^2: sorting array of hashes using multiple keys conditionally
by Marshall (Canon) on Dec 05, 2009 at 21:35 UTC
    I'm wondering why || doesn't cause a precedence problem here? I use "or" like below in this situation.

    update: looked up precedence table and spaceship (<=>) and cmp have much higher priority than I thought, so I guess this is ok either way.

    @a = sort { $a->{key1} <=> $b->{key1} or $a->{key2} <=> $b->{key2} } @a;
      I'm wondering why || doesn't cause a precedence problem here?

      Because the  || operator has lower precedence than the comparison operators like  <=> for example.    See perlop.

Re^2: sorting array of hashes using multiple keys conditionally
by mabman2 (Novice) on Dec 06, 2009 at 06:28 UTC
    Thanks, I've tried it and it does exactly what I'm looking for. Much simpler than I thought.