XLISP sort

Nyquist / XLISP 2.0 - Contents | Tutorials | Examples | Reference

# sort

Type: | - | function (subr)
Source: | - | xllist.c

## Syntax

(sort list test)
  list - a list containing elements to be sorted
  test - the test to use for the sort
  returns - the sorted list

## Description

The 'sort' function sorts the 'list' using the 'test' to order the list. The 'sort' function is destructive and modifies the 'list'.

## Examples

    (setq a '(3 1 4 1 5 9 6 7))          ; returns (3 1 4 1 5 9 6 7)

    (sort a '<)                          ; returns (1 1 3 4 5 6 7 9)

    (print a)                            ; returns (1 1 3 4 5 6 7 9)
                                         ; notice that A is modified

    (sort a '>)                          ; returns (9 7 6 5 4 3 1 1)

    (sort '("a" "bar" "foo") 'string>)   ; returns ("foo" "bar" "a")

XLISP Bug

Nyquist 'sort' returns the proper value, but improperly modifies the symbol or the actual 'list', for example:

    (setq a '(3 1 4 1 5 9 6 7))  => (3 1 4 1 5 9 6 7)
    (sort a '<)                  => (1 1 3 4 5 6 7 9)  ; OK
    a                            => (3 4 5 6 7 9)      ; BUG

But this way it works:

    (setq a '(3 1 4 1 5 9 6 7))  => (3 1 4 1 5 9 6 7)
    (setq a (sort a '<))         => (1 1 3 4 5 6 7 9)
    a                            => (1 1 3 4 5 6 7 9)

Common Lisp: Common Lisp allows for a ':key' keyword, which allows a specified function to be run before the ordering takes place, which XLISP does not support.

See the sort function in the XLISP 2.0 manual.

Back to Top

Nyquist / XLISP 2.0 - Contents | Tutorials | Examples | Reference