Sort
Sort
Sort[ list ] returns a copy of a list sorted in ascending numerical order.
Usages
Sort[ list ]
Sort[ list ] returns a new list with the elements of list ordered from smallest to largest numerical value. The original list is not changed. Elements can be any expression with a numerical value, such as integers, decimals, fractions, roots or Pi.
| Example | in | out |
|---|---|---|
| Sort a list of integers. Equal values are all kept. | Sort[ {5, 2, 5, 1} ] |
{1, 2, 5, 5} |
| Sort a list of mixed numerical values. | Sort[ {3, 1/2, -2.5, Sqrt[2], Pi} ] |
{-2.5, 1/2, Sqrt[2], 3, Pi} |
Sort a list stored in a script variable. The variable L itself is unchanged. |
L := {4, 9, 1} |
S = {1, 4, 9} |
Details
- Elements without a numerical value (such as
xor2x) are sorted as if their value is0. - Equal values keep their original order. This also applies to elements without a numerical value.
- Empty list:
Sort[ {} ]returns{}. - Order: sorting is always ascending. There is no option for descending order.
| Example | in | out |
|---|---|---|
x is sorted as 0. |
Sort[ {x, 7, -3, 5} ] |
{-3, x, 5, 7} |
b and a are both sorted as 0 and keep their original order. |
Sort[ {b, a, 2, -1} ] |
{-1, b, a, 2} |
| Invalid input | Result |
|---|---|
Sort[ 5 ] |
Error: the argument should be a list |
Sort[ {1, 2}, {3} ] |
Error: the number of arguments should be 1 |