Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Sorting Arrays

by nardo (Friar)
on Jun 26, 2000 at 09:27 UTC ( [id://19807]=note: print w/replies, xml ) Need Help??


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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://19807]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-25 08:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found