一个结构中包含一个字符串成员,以下代码用来实现按字符串的排序结构体:
typedef struct
{
char mssi_id[5];
char individua_name[16];
}DSC_INDIVIDUA_INFO;
//把小写的字母转化成大写,然后再作比较
static char* ChangeStringFristCharacter(char* dest, const char* src)
{
char *p = dest;
strcpy(dest, src);
if(dest[0] >= 'a' && dest[0] <= 'z'){
dest[0] = dest[0] - ('a' -'A') ;
}
return p;
}
DSC_INDIVIDUA_INFO* IndividualContactList_Sort(DSC_INDIVIDUA_INFO *list, int num)
{
DSC_INDIVIDUA_INFO TempBuffer;
int i, j;
char Temp1[16], Temp2[16];
for(i = 0; i < num - 1; i++){
for(j = i + 1; j < num; j++){
ChangeStringFristCharacter(Temp1, (char const *)list[i].individua_name);
ChangeStringFristCharacter(Temp2, (char const *)list[j].individua_name);
if (strcmp(Temp1, Temp2) > 0){
memset(&TempBuffer, 0, sizeof(DSC_INDIVIDUA_INFO));
memcpy(&TempBuffer, &list[i], sizeof(DSC_INDIVIDUA_INFO));
memcpy(&list[i], &list[j], sizeof(DSC_INDIVIDUA_INFO));
memcpy(&list[j], &TempBuffer, sizeof(DSC_INDIVIDUA_INFO));
}
}
}
return NULL;
}
DSC_INDIVIDUA_INFO INDIVIDUA_info_array[80];
IndividualContactList_Sort(INDIVIDUA_info_array, 80);