sortIndices function


  • float[] sortIndices(array)


  1. array—(float[], string[])
    Array to be sorted.


The sortIndices function returns the indices of sorted values in array.

array =                                     ["c","b","d","a"]
sortIndices(array)                          [3,1,0,2]
array[sortIndices(array)]                   ["a","b","c","d"]

Duplicate values are sorted in the order of appearance.

array =                                     [2,1,3,3,1]
sortIndices(array)                          [1,4,0,2,3]
array[sortIndices(array)]                   [1,1,2,3,3]

Each row is sorted separately. The returned index array has the same dimensions as array.

array =                                     ["e","c","d";
sortIndices(array)                          [1,2,0;
array[sortIndices(array)]                   ["c","d","e";



Ascending and descending sorting

reverse(array)  = array[size(array)-1:-1:0]

sortAsc(array)  = array[sortIndices(array)]
sortDesc(array) = reverse(sortAsc(array))

const a = ["a", "c", "b"]
const b = sortAsc(a)       // [a,b,c]
const c = sortDesc(a)      // [c,b,a]

An array is indexed with sorted indexes which sorts all values in ascending order. The resulting array can be reversed for a descending sort order. Note that this example only applies to 1d arrays with 1 row.

Finding minimum and maximum

findMin(array) = sortIndices(array)[0]
findMax(array) = sortIndices(array)[size(array)-1]

min(array) = array[findMin(array)]
max(array) = array[findMax(array)]

const a = [1, 4, -1, 9]
const b = min(a)           // -1
const c = max(a)           // 9

The index of the minimum value in array is the first element in the sorted indexes array. Note that this example only applies to 1d arrays with 1 row. 2d arrays must be "flattened" first, i.e. reshaped to an array with one row:

flatten(array) = array[0:size(array)-1]

findMin(array) = sortIndices(flatten(array))[0]
findMax(array) = sortIndices(flatten(array))[size(array)-1]