in reply to Sorting Arrays

@sorted = sort {@{$b}[2] cmp @{$a}[2]} @array;

Replies are listed 'Best First'.
RE: Re: Sorting Arrays
by merlyn (Sage) on Jun 26, 2000 at 20:20 UTC
    Nope. You're using an array slice @x[$y] where you really want an element $x[$y]. And as pointed out elsewhere, you're using cmp where you want <=>.

    -- Randal L. Schwartz, Perl hacker

(zdog) Re: (2) Sorting Arrays
by zdog (Priest) on Jun 26, 2000 at 10:51 UTC
    Thanks for the help. And now that I think about it, I probably should have got it on my own, but when I go into unchartered territory (functions that I have never used before), my brain freezes. Thanks again.

    -- zdog (Zenon Zabinski)
       Go Bells!! '¿'

      if array#num is really a number, <=> might be a better option ...
      @array = sort { $$a[2] <=> $$b[2] } @array;
      --
      Greg McCarroll
      http://www.mccarroll.uklinux.net
Re: Re: Sorting Arrays
by PhosphoricX (Sexton) on Apr 05, 2004 at 07:44 UTC
    Thanks for the tip. I used this to sort my array[][][]. I was able to sort my list by an object property as follows:
    @memory_blocks = sort {hex(${$b}[0][0]->address) <=> hex(${$a}[0][0]-> +address)} @memory_blocks;
    Hope that might help someone else who gets stuck. Thanks again!