SortI
SortI
SortI[ list ] returns the indices of the elements of a list, in the order that sorts the list ascending.
Usages
SortI[ list ]
SortI[ list ] returns a list of positions (starting at 1). The first position is that of the smallest element of list, the second position that of the next smallest element, and so on. Use it when you need to know where the sorted elements were, for example to sort a second list in the same order.
| Example | in | out |
|---|---|---|
The smallest element is at position 1, then position 3, then position 2. |
SortI[ {10, 30, 20} ] |
{1, 3, 2} |
| Mixed numerical values. | SortI[ {3, 1/2, -2.5, Sqrt[2], Pi} ] |
{3, 2, 4, 1, 5} |
| Use the indices to look up the elements in sorted order. | L := {40, 10, 30} |
I = {2, 3, 1}S = {10, 30, 40} |
Details
- Equal values keep their original order:
SortI[ {5, 2, 5, 1} ]returns{4, 2, 1, 3}. - Empty list:
SortI[ {} ]returns{}. - Order: always ascending. There is no option for descending order.
| Invalid input | Result |
|---|---|
SortI[ 5 ] |
Error: the argument should be a list |
SortI[ {x, 7, -3, 5} ] |
Error (see the warning below) |
Warning
Every element of the list must have a numerical value. Unlike Sort, which treats an element such as x as 0, SortI stops the script with an error when the list contains an element without a numerical value.