And for a more immediate answer to your question, check out these FAQ entries:
For some more examples, check out perldoc on sort.The gist of it all is this: the sort block is called to compare two items from the list-to-sort. It will actually be called many times for this one sort statement, in order to establish a complete sorting order for the whole list. The value returned by the block reflects whether the first item ($a) is to be considered bigger or smaller than the second item ($b), depending on the sign of the result. If they are equal, return 0. Your code block should return the same sign as cmp and <=> return, which is not a coincidence, but by design. You might as well use - (minus) for comparing numbers, the results would be quite the same.
This code snippet simply numerically sorts the passed numbers, resulting in a rather boring list.print join " ", sort { $a - $b } 1, 3, 2, 5, 4;
In reply to Re: question re: hash sorting
by bart
in thread question re: hash sorting
by Grygonos
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |