我希望按照第一个数组对第二个数组进行排序。例如
first = {1,8,7,2,4}
second = {9,7,2,10,3}
Run Code Online (Sandbox Code Playgroud)
我希望第一个保持不变,第二个以与第一个相同的相对顺序进行排序。即,最低值在索引0处,第二最低值在索引3处,第三最低值在索引4处,依此类推,等等
second = {2,10,9,3,7}
Run Code Online (Sandbox Code Playgroud)
我已经尝试了以下代码
#include <stdio.h>
typedef struct
{
int num;
int pos;
}ArrType;
ArrType arrA[5] = {{1,0},{8,1},{7,2},{2,3},{4,4}};
ArrType arrB[5] = {{9,0},{7,1},{2,2},{10,3},{3,4}};;
int cmparr(const void *a, const void *b)
{
ArrType *tmpa, *tmpb;
tmpa = (ArrType*) a;
tmpb = (ArrType*) b;
return(arrA[tmpa->pos].num - arrA[tmpb->pos].num);
}
int main(void)
{
int i;
qsort(arrB,5, sizeof(ArrType), cmparr);
for (i=0; i<5; i++)
{
printf ("%d ",arrB[i].num);
}
return (0);
}
Run Code Online (Sandbox Code Playgroud)
实际输出为
9 10 3 2 7
我对另一种数据结构持开放态度,但 …