News flash: I'm going to Australia in February, so this Web site may be switched off at any time.


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
end
The 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.
Next: Using the table type in Icon
See also: Using the Icon programming language
Previous: Generating the elements of an Icon list

John W. Shipman, john@nmt.edu