# sortIndices 函数

### 语法

• float[] sortIndices(array)

### 参数

1. array—(float[], string[])
要排序的数组。

### 描述

sortIndices 函数返回 array 中排序值的指数。

``````array =                                     ["c","b","d","a"]

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

array[sortIndices(array)]                   ["a","b","c","d"]``````

``````array =                                     [2,1,3,3,1]

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

array[sortIndices(array)]                   [1,1,2,3,3]``````

``````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"]``````

### 示例

#### 升序和降序排序

``````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]``````

array 使用排序索引建立索引，排序索引按升序对所有值进行排序。 结果数组可以反转为降序排序。 请注意，此示例仅适用于具有 1 行的 1d 数组。

#### 查找最小值和最大值

``````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``````

array 中最小值的索引是已排序索引数组中的第一个元素。 请注意，此示例仅适用于具有 1 行的 1d 数组。 2d 数组必须首先“展平”，即重新修整为一行数组：

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

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