On Thu, 2004-02-12 at 15:46, Uri Guttman wrote:
> >>>>> "LP" == Luke Palmer <[EMAIL PROTECTED]> writes:

>   LP> Ok, I have to say, that's pretty good.  Er, really good.  I like it a
>   LP> lot.
> 
> how do you select descending order? and how do you selecte that per key?
> you can't provide a binary operator without also providing the
> order. and what about different key types? the <=> and cmp operators are
> not enough information needed to do complex sorts. collating sequences
> are another issue. you need to have that info on a perl key basis. 

You know, I have trouble typing "per" when I'm thinking "perl" too ;-)

I think my example addressed all of your concerns, and did it in Perl 5.
Once again, that was:

        sub sortpairs(&@) {
          my $comp = shift;
          my %pairs = @_;
          return map {$pairs{$_}} sort {$comp->()} keys %pairs;
        }
        
        @new1 = sortpairs {$a <=> $b} map {($_->computekey,$_)} @old1;
        @new2 = sortpairs {$a cmp $b} map {(lc($_),uc($_))} @old2;

So, in order, your questions were:

      * how do you select descending order?
        You reverse the $a and $b in the first parameter to sortpairs
      *  and how do you selecte that per key?
        Ok, I was only addressing one key. But, it's not hard to
        generalize the idea, and certainly Perl 6 gives you the tools to
        do so easily.
      * you can't provide a binary operator without also providing the
        order.
        Correct
      * and what about different key types? the <=> and cmp operators
        are not enough information needed to do complex sorts.
        That's fine, you can plug in a call to Alien::Headed::Babies
        inside the closure for all I care.
      * collating sequences are another issue. you need to have that
        info on a perl key basis.
        Give me an example?
      * multiple keys with each having different comparisons and
        different sort orders.

Ok let's delve into it then:

        sub sortpairs(&@){
          my $comp = shift;
          my @elements = @_;
          return map {my $e=$elements[$_];$e->[$#{$e}]} sort {
             my @akeys = @{$elements[$a]}[0..$#{$elements[$a]}-1];
             my @bkeys = @{$elements[$b]}[0..$#{$elements[$b]}-1];
             for(my $i=0;$i<@akeys;$i++) {
               my $dir = $comp->($akey[$i],$bkey[$i]);
               return $dir if $dir;
             }
             return 0;
          } 1..scalar(@elements);
        }
                                                                                       
 
        @new1 = sortpairs {$_[0] <=> $_[1]} map {[$_,$_]} @old1;
        @new2 = sortpairs {$_[0] <=> $_[1] || $_[4] cmp $_[3]}  map {[getkeys($_),$_]} 
@old2;

The second example really illustrates the point that you can swap the
direction of key order and mechanism to compare them at your whim.

Now, you just need to call sortpairs with any array of arrays of keys
(with trailing value).

-- 
Aaron Sherman <[EMAIL PROTECTED]>
Senior Systems Engineer and Toolsmith
"It's the sound of a satellite saying, 'get me down!'" -Shriekback


Reply via email to