in reply to Complicated, multi-level array sorting

? 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.

Replies are listed 'Best First'.
•Re: Re: Complicated, multi-level array sorting
by merlyn (Sage) on May 25, 2003 at 14:29 UTC
    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.

Re: Complicated, multi-level array sorting
by Abigail-II (Bishop) on May 25, 2003 at 14:27 UTC
    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