Difference between revisions of "PGsort"

From WeBWorK_wiki
Jump to navigation Jump to search
(from ur manpages)
(No difference)

Revision as of 20:42, 18 November 2009

PGSort

Description

General sorting macro

Syntax

PGsort( &sort_subroutine, @list);

Params

&sort_subroutine is a subroutine of two variables which defines order. It's output must be -1, 0, 1 depending on whether the first variable is less than, equal to or greater than the second.
@list is the list to be sorted.

Returns

Returns list of elements in @list, ignoring duplicates. The order is unspecified.

Examples

join(" ",PGsort( sub {$_[0] <=> $_[1]} , (23,2,10,11,11,31); ); returns "2 10 11 11 23 31"

Notes

Using the perl sort directly in problems is not allowed for two reasons. The first is that the hardwired $a and $b variables used by sort can interfere with other values assigned to $a and $b. The second is that it is pretty easy to create closed loops with sort that hang the program. Currently nothing is done to prevent this, but later we may wish to modify num_sort and lex_sort to catch this problem.