Quantcast

Documentation Center

  • Trial Software
  • Product Updates

sort

Use only in the MuPAD Notebook Interface.

This functionality does not run in MATLAB.

Syntax

sort(list, <f>)

Description

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

sort sorts the list in ascending order.

If you do not specify a procedure f, the sort command uses the following rules for sorting the lists:

  • The command sorts a list of real numbers (Type::Real) numerically.

  • The command sorts a list of character strings alphabetically.

  • The command sorts an outer list containing inner lists with numeric first entries by these numeric first entries. See Example 4.

  • In all other cases, the command sorts a list according to the internal order: sort(list) is equivalent to sort(list, sysorder). All MuPAD® sessions use the same internal order. Between different versions of MuPAD, internal order might change.

When you sort strings, uppercase letters have a preference over lowercase letters. For example, Z appears before abc.

You can specify a procedure f to define the sorting criteria. sort calls the procedure f for every pair of the entries of the list. f must return a Boolean expression that the bool command can evaluate to TRUE or FALSE. If for the pair of entries the procedure f(x, y) returns TRUE, the sorted list displays x to the left of y. Otherwise, x appears to the right of y. The entries of the sorted list L := sort(list, f) satisfy bool(f(L[i], L[j])) = TRUE for i < j.

If two entries of a list are equal by the sorting criteria f, the sort command can swap these entries. For example, if you sort polynomials by their degrees, the sort command can return the polynomials with the same degree in the order different from their order in the input.

sort can be overloaded by kernel domains. For example, use the function DOM_SET::sort to sort sets. See Example 3

The average runtime to sort a list containing n entries is O(nlog(n)).

Examples

Example 1

The sort command sorts real numbers (type Type::Real) numerically:

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

The sort command sorts strings alphabetically:

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

If a list contains other types of objects the sort command sorts a list according to the internal order. The command also applies internal order to sort the lists with mixed types of entries:

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

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

Example 2

Define your own criteria to sort a list. For example, sort the entries by their absolute values:

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

Example 3

When sorting sets, the sort command returns a list as a result:

sort({3, 12, 5, 30, 6, 43})

The sorted set is equivalent to the corresponding sorted list:

bool(sort({3, 12, 5, 30, 6, 43}) = sort([3, 12, 5, 30, 6, 43]))

To sort other data types, implement a sort-slot for them:

unprotect(DOM_INT):
DOM_INT::sort :=
proc(n)
  local str, i;
begin
  str := expr2text(n);
  text2expr(_concat(op(sort([str[i] $ i = 1..length(str)]))))
end:
sort(1703936)

delete DOM_INT::sort:  protect(DOM_INT):

Example 4

If the list contains lists as entries, and all the inner lists start with numbers, the sort command uses these numbers to sort the outer list:

sort([[10 - i, i*x^i] $ i = 1..9])

Compare the sorted list with the internal order of its entries:

sort([[10 - i, i*x^i] $ i = 1..9], sysorder)

Parameters

list

A list of arbitrary MuPAD objects

f

A procedure defining the ordering

Return Values

List.

Overloaded By

list

Algorithms

To implement the sort function, MuPAD uses a version of the Quicksort algorithm.

See Also

MuPAD Functions

Was this topic helpful?