---
title: SortIndex
description: "带索引的向量排序，包含升序和降序。如果参与排序的向量值有相同值，则这些相同值所对应的索引不排序，即排序算法是不稳定的，对相同值排序，排序后的索引顺序与排序之前的顺序不同。"
url: https://www.hikunpeng.com/document/detail/zh/kunpengboostkithistory/240RC1/accel/kunpengaccel_hmpp_06_0171.html
sourcePath: /source/zh/kunpengboostkithistory/240RC1/accel/kunpengaccel_hmpp_06_0171.html
indexId: 245fdd73267fbfb34bd5fbdfb0f4c245c07e30fceb5bf00a73b064bada65cd0777
---
# SortIndex

带索引的向量排序，包含升序和降序。如果参与排序的向量值有相同值，则这些相同值所对应的索引不排序，即排序算法是不稳定的，对相同值排序，排序后的索引顺序与排序之前的顺序不同。

函数接口声明如下：- 整型数的升序排序：
  HmppResult HMPPS_SortIndexAscend_8u_I(uint8_t* srcDst, int32_t dstIdx, int32_t len);

  HmppResult HMPPS_SortIndexAscend_16u_I(uint16_t* srcDst, int32_t dstIdx, int32_t len);

  HmppResult HMPPS_SortIndexAscend_16s_I(int16_t* srcDst, int32_t dstIdx, int32_t len);

  HmppResult HMPPS_SortIndexAscend_32s_I(int32_t* srcDst, int32_t dstIdx, int32_t len);

- 浮点数的升序排序：
  HmppResult HMPPS_SortIndexAscend_32f_I(float* srcDst, int32_t dstIdx, int32_t len);

  HmppResult HMPPS_SortIndexAscend_64f_I(double* srcDst, int32_t dstIdx, int32_t len);

- 整型数的降序排序：
  HmppResult HMPPS_SortIndexDescend_8u_I(uint8_t* srcDst, int32_t dstIdx, int32_t len);

  HmppResult HMPPS_SortIndexDescend_16u_I(uint16_t* srcDst, int32_t dstIdx, int32_t len);

  HmppResult HMPPS_SortIndexDescend_16s_I(int16_t* srcDst, int32_t dstIdx, int32_t len);

  HmppResult HMPPS_SortIndexDescend_32s_I(int32_t* srcDst, int32_t dstIdx, int32_t len);

- 浮点数的降序排序：
  HmppResult HMPPS_SortIndexDescend_32f_I(float* srcDst, int32_t dstIdx, int32_t len);

  HmppResult HMPPS_SortIndexDescend_64f_I(double* srcDst, int32_t dstIdx, int32_t len);


#### 参数

| 参数名 | 描述 | 取值范围 | 输入/输出 |
| --- | --- | --- | --- |
| srcDst | 指向原址操作向量的指针。 | 非空 | 输入/输出 |
| dstIdx | 指向排序后索引的指针。 | 非空 | 输出 |
| len | 向量长度。 | (0, INT\_MAX] | 输入 |


#### 返回值

- 成功：返回HMPP_STS_NO_ERR。
- 失败：返回错误码。


#### 错误码

| 错误码 | 描述 |
| --- | --- |
| HMPP\_STS\_NULL\_PTR\_ERR | srcDst为空指针。 |
| HMPP\_STS\_SIZE\_ERR | len小于或等于0。 |


#### 示例

```
#define BUFFER_SIZE_T 9
void Sort_Example() {
uint8_t src[BUFFER_SIZE_T] = {255, 5, 254, 0, 253, 1, 252, 2, 251};
int32_t *dstIdx = HMPPS_Malloc_32s(BUFFER_SIZE_T);
int32_t i;
HmppResult result = HMPPS_SortAscend_8u_I(src, dstIdx ,BUFFER_SIZE_T);
if (result == HMPP_STS_NO_ERR) {
printf("result = %d\n", result);
printf("dst = ");
for (i = 0; i < BUFFER_SIZE_T; i++) {
printf("%d ", src[i]);
}
printf("\n");
printf("dstIdx = ");
for (i = 0; i < BUFFER_SIZE_T; i++) {
printf("%d ", dstIdx[i]);
}
printf("\n");
}
HMPPS_Free(dstIdx);
}
```

运行结果：

```
result = 0
dst = 0 1 2 5 251 252 253 254 255
dstIdx = 3 5 7 1 8 6 4 2 0
```
