Sort objects by an index function

MuPAD® notebooks will be removed in a future release. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.


prog::sort(list, func, <Reverse>, <p1, p2, …>)


prog::sort(list, func) applies the function func to any object of the list list and returns a list with the given objects sorted by the order of the indices calculated by func.

func is applied only once to any object in list.

If optional arguments are present, then the indices are computed from the objects x of list by f(x, p1, p2, ...).

An alternative call to prog::sort is the call sort(list, (X, Y) -> func(X) <= func(Y)).


Example 1

Sort a list of expressions by their length:

prog::sort([2*x, x - 4, sin(x), x + y + z], length)

Sort a list of lists by the number of operands, with descending order:

prog::sort([[1,2,3],[4,2],[0 $ 10],[]], nops, Reverse)



A list of MuPAD® objects


A function that must return a numerical value, when applied to any object of the list


An option

p1, p2, …

Any MuPAD objects accepted by func as additional parameters



prog::sort compares the calculated indices in reverse order.

Return Values

List with the same objects as the given list

See Also

MuPAD Functions