## `sort` -- sort a list

#### Introduction

`sort(`list`)` returns a sorted copy of the list.

#### Call(s)

`sort(list <, f>)`

#### Parameters

`list` | - | a list of arbitrary MuPAD objects
`f` | - | a procedure defining the ordering

#### Returns

a list.

### Overloadable:

`list`

#### Related Functions

`sysorder`

#### Details

- `sort` sorts the list in ``ascending order''.
- If no procedure `f` is specified by the user, lists are sorted as follows:
  - A list with real numbers of syntactical type `Type::Real` is sorted numerically.
  - A list of character strings is sorted lexicographically.
  - In all other cases, the list is sorted according to the system's internal order, i.e., `sort(`list`)` is equivalent to `sort(`list, sysorder`)`.

    The internal order is a well-ordering. It is not session dependent, but may differ between different MuPAD versions. No special features of the internal sorting mechanism should be assumed by the user. Such sorting may be useful to produce a unique representation of lists.

- When strings are compared, capital letters are sorted in front of small letters. E.g., `"Z"` is smaller than `"abc"`.

- Sets and tables do not have a unique internal order (`sysorder`). Consequently, sorting does not lead to a unique ordering, if elements of the list are sets or tables, or contain sets or tables as (sub)operands. Cf. example 2.

- A procedure `f` may be specified to define the sorting criteria. It is used to compare the ordering of pairs of list elements and is called in the form `f(x, y)` with elements `x`, `y` from the list. It must return a Boolean expression that can be evaluated to either `TRUE` or `FALSE`. `TRUE` indicates that `x` is to be sorted left of `y`. Consequently, the elements of the ordered list `L := sort(list, f)` satisfy `bool``(f(L[i], L[j])) = TRUE` for `i < j`.

  If the ordering provided by `f` is not a well-ordering, sorting is not 'stable' and elements with the same order may be swapped.

- The mean run time for sorting n elements is O(n*log(n)).
- `sort` is a function of the system kernel.

#### Example 1

Real numbers of syntactical type `Type::Real` are sorted numerically:

    >> sort([4, -1, 2/3, 0.5])

                                 [-1, 0.5, 2/3, 4]

Strings are sorted lexicographically:

    >> sort(["chip", "alpha", "Zip"])

                             ["Zip", "alpha", "chip"]

Other types of objects are sorted according to their internal ordering. This also holds for lists with elements of different types:

    >> sort([4, -1, 2/3, 0.5, "alpha"])

                            ["alpha", -1, 4, 0.5, 2/3]

    >> sort([4, -1, 2/3, 0.5, I])

                               [-1, 4, 0.5, 2/3, I]

#### Example 2

There is no unique internal order for sets and tables:

    >> sort([{1}, {2}]) <> sort([{2}, {1}])

                             [{1}, {2}] <> [{2}, {1}]

    >> sort([table("a" = 42), table("a" = 43)]) <>
       sort([table("a" = 43), table("a" = 42)])

          -- table(       table(      --
          |    "a" = 42 ,   "a" = 43   | <>
          -- )            )           --

             -- table(       table(      --
             |    "a" = 43 ,   "a" = 42   |
             -- )            )           --

#### Example 3

The following list is sorted according to a user-defined criteron:

    >> sort([-2, 1, -3, 4], (x, y) -> abs(x) < abs(y))

                                  [1, -2, -3, 4]

#### Background

- A variant of the Quicksort algorithm is used.

#### Changes

- The internal order has changed with the present MuPAD version.

Do you have questions or comments?
Copyright © SciFace Software GmbH & Co. KG 2000