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

Okay simple question: What's the most efficient way to turn %arr1 into %arr2?

What I did to convert them was basically reorder by 'cat', then by 'name', and then by 'rating'.

%arr1 = ( { id => 1, name => 'Frank', cat => 'Cat1', rating => 5 }, { id => 2, name => 'John', cat => 'Cat1', rating => 4 }, { id => 3, name => 'Smith', cat => 'Cat1', rating => 3 }, { id => 4, name => 'Smith', cat => 'Cat2', rating => 8 }, { id => 5, name => 'John', cat => 'Cat2', rating => 9 }, { id => 6, name => 'Frank', cat => 'Cat2', rating => 10 }, { id => 7, name => 'Frank', cat => 'Cat3', rating => 1 }, { id => 8, name => 'Smith', cat => 'Cat3', rating => 8 }, { id => 9, name => 'Frank', cat => 'Cat3', rating => 2 }, { id => 10, name => 'John', cat => 'Cat1', rating => 3 } ); %arr2 = ( { id => 1, name => 'Frank', cat => 'Cat1', rating => 5 }, { id => 10, name => 'John', cat => 'Cat1', rating => 3 }, { id => 2, name => 'John', cat => 'Cat1', rating => 4 }, { id => 3, name => 'Smith', cat => 'Cat1', rating => 3 }, { id => 6, name => 'Frank', cat => 'Cat2', rating => 10 }, { id => 5, name => 'John', cat => 'Cat2', rating => 9 }, { id => 4, name => 'Smith', cat => 'Cat2', rating => 8 }, { id => 7, name => 'Frank', cat => 'Cat3', rating => 1 }, { id => 9, name => 'Frank', cat => 'Cat3', rating => 2 }, { id => 8, name => 'Frank', cat => 'Cat3', rating => 8 }, );
Any help will be appreciated greatly!

Thanks,
--nutshell

Replies are listed 'Best First'.
Re: Complicated, multi-level array sorting
by arthas (Hermit) on May 25, 2003 at 13:56 UTC
    ? Are these array or hashes? They seem like array, but the % symbol then makes them hashes.

    Supposing they're arrays (@arr1 and @arr2), what I'll do is sort three times:
    @arr2 = sort { $$a{'rating'} cmp $$b{'rating'} } @arr1; @arr2 = sort { $$a{'name'} cmp $$b{'name'} } @arr2; @arr2 = sort { $$a{'cat'} cmp $$b{'cat'} } @arr2;
    There's surely a shorter way, although as far as efficiency is concerned I doubt you can go much farther. Then, I may be wrong. ;)

    Michele.
      That actually won't work for classic Perl, because the sort is not guaranteed stable. That is, when the sort comparison returns 0, the sort operator is free to place the left item before the right, or vice versa. So your third sort might wipe out the second sort, and the second sort might have already wiped out the first sort.

      You need to do the comparison all at once:

      @output = sort { $a->{cat} cmp $b->{cat} or $a->{name} cmp $b->{name} or $a->{rating} <=> $b->{rating} # rating looks numeric } @input;
      P.S. Modern Perl has a stable sort by default, but may be influenced at a distance to be an unstable sort, so it's best to pretend that this isn't so unless you control the entire program and can also ensure that your code will never run on older Perl versions.

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.

      More efficient is to use just one sort:
      @arr2 = sort {$$a {cat} cmp $$b {cat} || $$a {name} cmp $$b {name} || $$a {rating} cmp $$b {rating}} @arr1;

      Then you would only spend time comparing names and ratings if the 'cat's are identical.

      Abigail

Re: Complicated, multi-level array sorting
by nutshell (Beadle) on May 25, 2003 at 13:58 UTC
    Whoops! I did mean @arr1 and @arr2.

    Sorry about that.

    --nutshell