qsort记录(字符数组)按降序排列

时间:2013-11-27 19:37:44

标签: c sorting arrays qsort

我正在尝试使用qsort以降序打印和排序记录的动态内存。我正在使用命令行来确定如何对记录进行排序。

打印记录的代码:

void print_records_by_name(record_list *list, int argc, const char *argv[])
{
    size_t i;

    if (argc == 2) {
        if (strcmp(argv[1], "+n") == 0) {
            qsort(list->data, list->nused, sizeof(record), sort_name_asc);
        } else if (strcmp(argv[1], "-n") == 0) {
            qsort(list->data, list->nused, sizeof(record), sort_name_des);
        }
    }

    for (i = 0; i < list->nused; i++) {
        fprintf(stdout, "%s %s %d\n", list->data[i].name.last, list->data[i].name.first, list->data[i].score);
    }
}

我有用于升序排序的代码:

int sort_name_asc(const void *p, const void *q)
{
    const record *pp = p;
    const record *qq = q;
    int n1, n2;

    n1 = strcmp(pp->name.first, qq->name.first);
    n2 = strcmp(pp->name.last, qq->name.last);

    if (n2 == 0) {
        return n1;
    }

    return n2;
}

这就是我的降序顺序:

int sort_name_des(const void *p, const void *q)
{
    const record *pp = p;
    const record *qq = q;
    int n1, n2;

    n1 = strcmp(qq->name.first, pp->name.first); /*just switched the pp and qq*/
    n2 = strcmp(qq->name.last, pp->name.last); /*just switched pp and qq*/

    if (n2 != 0) {
        return n1;
    }

    return n2;
}

我编译了它并没有用,任何想法怎么做?

由于

2 个答案:

答案 0 :(得分:0)

由于我不 知道record_list *list的样子,即:

我不确定语句qsort(list->data, list->nused, sizeof(record), sort_name_asc);是否正确,list->data实际上是一个数组,还是list->used实际上是数组中项目的数量,... < / p>

所以,我只需 按升序和降序说明字符串比较,希望对您有所帮助:

注意:比较函数 与返回值除外...

#include <ansi_c.h>

static int sortstringAscending( const void *str1, const void *str2 );
static int sortstringDescending( const void *str1, const void *str2 );

int main(void)
{
    int i;
    char string1[10][4] = {"eee","bbb","jjj","ddd","aaa","fff","ggg","iii","hhh","ccc"};

    //Important that you populate the qsort function with the right values
    //that is qsort(StringArray, numElemetsInArray, sizeOfEachElement, sortFunction);

    qsort(string1, 10, 4,sortstringAscending);
    for(i=0;i<10;i++) printf("string %d is: %s\n", i, string1[i]);
    printf("\n");
    qsort(string1, 10, 4,sortstringDescending); 
    for(i=0;i<10;i++) printf("string %d is: %s\n", i, string1[i]);

    getchar();

    return 0;
}

static int sortstringAscending( const void *str1, const void *str2 )
{
    const char *rec1 = (const char *)str1;
    const char *rec2 = (const char *)str2;
    int val = strcmp(rec1, rec2);

    return val;
}

static int sortstringDescending( const void *str1, const void *str2 )
{
    const char *rec1 = (const char *)str1;
    const char *rec2 = (const char *)str2;
    int val = strcmp(rec1, rec2);

    return -1*val;//Because of the way strcmp() works, 
                  //this simple change causes the results 
                  //to be reversed for a string array
}

答案 1 :(得分:0)

由于您在第二个函数中切换了指针,因此需要保持相同的逻辑。

if (n2 == 0) {
    return n1;
}

或者简单地说:

int sort_name_des(const void *p, const void *q)
{
    return -sort_name_asc(p, q);
}