Sorting an Icon list
Icon's built-in ``
sort()'' function can be used to
order the elements of a list. The expression
sort ( L )returns a list with the same number of elements as L, except they are sorted. This makes sense only for certain types---integers, reals, and strings.
As an example, here is a program that writes the lines from its input in sorted order:
procedure main() lines := ; # Create an empty list while put ( lines, read ( ) ); # Store all lines every write ( ! sort ( lines ) ); # Sort and write endThe second line reads each input line and adds it to the list. In the third line, the expression ``
sort(lines)'' returns a list of the lines in sorted order; the ``
!'' operator generates the lines from the sorted list in order, and then they are written to the output.
John Shipman, email@example.com
Last updated: 1998/02/08 23:18:24 UT