SPUG: sorting an array

Kevin Fink kevin at n2h2.com
Mon Jun 19 21:04:12 CDT 2000


Careful, the default sort is a "string" sort, not a "numeric" sort:

perl -e 'print join(" ",sort 9,10,11),"\n"';

10 11 9

perl -e 'print join(" ",sort { $a <=> $b } 9,10,11),"\n"';

9 10 11

Kevin

On Mon, 19 Jun 2000, Ryan Ames wrote:

> thanks....
> 
> -----Original Message-----
> From: Sanford Morton [mailto:smorton at pobox.com]
> Sent: Monday, June 19, 2000 6:55 PM
> To: Ryan Ames
> Subject: SPUG: sorting an array
> 
> 
> Ryan Ames writes:
>  > I am kind of a newbie to perl and was wondering if there was a quick way
> to
>  > do a sort on an array of long ints.  
>  > 
>  > My array would look something like this.
>  > 
>  > 	@myArray = (123000, 122956, 239210, 128967, 543902, 209031, 333910);
> 
> Heheh, welcome to Perl, which makes easy things easy:
> 
>   @mySortedArray = sort @myArray;
>   for (@mySortedArray) { print "$_ "}'
>   ==> 122956 123000 128967 209031 239210 333910 543902
> 
> 
>  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>      POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
>  Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
>  For Subscriptions, Email to majordomo at pm.org:  ACTION  spug-list  EMAIL
>   Replace ACTION by subscribe or unsubscribe, EMAIL by your Email address
> 
> 

------------------------------------------------------------------------------
 Kevin Fink <kevin at n2h2.com>          N2H2, Creators of Bess and Searchopolis
 Chief Technology Officer             900 Fourth Avenue, Suite 3400
 http://www.n2h2.com/                 Seattle, WA 98164
 VOICE: 206-336-1501 / 800-971-2622   FAX: 206-336-1541
------------------------------------------------------------------------------


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
 For Subscriptions, Email to majordomo at pm.org:  ACTION  spug-list  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email address





More information about the spug-list mailing list