#include "stdafx.h"
#include "stdlib.h"
typedef struct tagSqort
{
int a;
int b;
}TestSqort;
TestSqort TestSqortNumber[] =
{
{10, 9},
{8,0},
{12,89},
{189, 9},
};
int compare(const void *a, const void *b)
{
TestSqort *Left = (TestSqort *)a;
TestSqort *Right = (TestSqort *)b;
if (Left->a < Right->a)
{
return 1;
}
else
{
return -1;
}
}
//头文件
int _tmain(int argc, _TCHAR* argv[])
{
int i = 0;
for (i = 0; i<4; i++)
{
printf("%d \n",TestSqortNumber[i].a);
}
qsort(TestSqortNumber, 4, sizeof(TestSqortNumber[0]), compare);
for (i = 0; i<4; i++)
{
printf("%d \n",TestSqortNumber[i].a);
}
return 0;
}
输出结果:
10
8
12
189
189
12
10
8
按照降序排列