CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
sorting a numerical array

 



waherne
Novice

Mar 11, 2000, 8:57 AM

Post #1 of 4 (840 views)
sorting a numerical array Can't Post

Hi,

I have an array e.g. {3,5,6,1,2,4}

and I want to sort it from highest number to lowest to get {6,5,4,3,2,1}.

Any ideas how I do this?

Willie


Cure
User

Mar 11, 2000, 9:06 AM

Post #2 of 4 (840 views)
Re: sorting a numerical array [In reply to] Can't Post

HI

you can do it this way:

@file = (3,5,6,1,2,4);
@file = @file [sort { $file[$b] <=> $file[$a] } 0 .. $#file ];
print @file;

Cure


japhy
Enthusiast

Mar 11, 2000, 9:24 AM

Post #3 of 4 (840 views)
Re: sorting a numerical array [In reply to] Can't Post

You don't need to sort via indices there. Just sort via $a and $b:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


@sorted = sort { $b <=> $a } 1,5,3,7,8,4,2;
</pre><HR></BLOCKQUOTE>


waherne
Novice

Mar 12, 2000, 5:04 AM

Post #4 of 4 (840 views)
Re: sorting a numerical array [In reply to] Can't Post

Thanks Folks

this worked a treat

Willie

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives