sortIndices function
float[] sortIndices(array)
array float[]string[] Array to be sorted.
Returns the indices of sorted values in array.
Basic sorting
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";
"b","a","c";
"d","a","d"]
sortIndices(array) = [1,2,0;
4,3,5;
7,6,8]
array[sortIndices(array)] = ["c","d","e";
"a","b","c";
"a","d","d"]
Related
Examples
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 indices which sorts all values in ascending order. The resulting array can be reversed for a descending sort order. This example only applies to 1D arrays with one 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 index array. For 2D arrays, flatten first:
flatten(array) = array[0:size(array)-1] findMin(array) = sortIndices(flatten(array))[0] findMax(array) = sortIndices(flatten(array))[size(array)-1]