Previous Page Next Page Contents

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

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

Changes




Do you have questions or comments?


Copyright © SciFace Software GmbH & Co. KG 2000