> @a= qw (68 3 5 67 54 23 69 );
> 
> @b = sort {-1} @a; ### what happens here !
> results = 5,3,68,67,69,23,54
>

Hey look, its reversed the order of the numbers either side
of the number 67!  Extremely useful :P  You shouldn't use
this sort, since it breaks for other quantities of numbers.

> @c = sort {$a <=> $b} @a; ### what happens here !
> results = 3,5,23,54,67,68,69

Sorts numerically.

> I know that sort by default sort in ascii order, I
> wanted to know what exactly happens to the "spaceship"
> operator. Could somebody help me to visualize what's
> taking place.
> 
> Thanks

Sort needs a subroutine which returns -1, 0, 1 as the
result of comparing two elements.  The values mean (where
$a and $b is a pair of values from the array):

-1 = put $a before $b
0  = doesn't matter which way to put $a and $b (i.e equal)
1  = put $a after $b

Now, the lovely <=> operator returns the following:

-1  - when $a is less than $b
0   - when $a is equal to $b
1   = when $a is greater than $b

Combine these:

When $a is less than $b put $a first.
When $a is same as $b leave alone.
When $a is greater than $b put $a second.

Result:

You have a numerical sort!  You don't really need to know
the impliementation of the sort (I was going to explain it,
but my explaination seemed too hazy).

Jonathan Paton

__________________________________________________
Do You Yahoo!?
Everything you'll ever need on one web page from News and Sport to Email and Music 
Charts
http://uk.my.yahoo.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to