使用qsort通过不同的变量对结构指针进行排序 [英] using a qsort to sort struct pointers by different variables

查看:96
本文介绍了使用qsort通过不同的变量对结构指针进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在指向结构的指针的上下文中理解c库qsort. 这是我要操作的现有代码:

I am attempting to understand the c library qsort in the context of pointers to structs. Here is the existing code that I would like to manipulate:

结构:

#define MAX_NAME 20
#define NUM_MONTHS 12

typedef struct EMP {
    char name[MAX_NAME+1];
    int monthSales[NUM_MONTHS];
    int total;
} Emp;

数据的全局初始化及其大小:

The global initialization of the data and its size:

Emp *data;//where all entries are kept
int empSize;

并且我已经构造了2个Emp指针数组,我想以不同的顺序引用数据:

and I have constructed 2 arrays of Emp pointers that I would like refer to the data in different orders:

Emp *nameArray[empSize];//an array of pointers to point to entries alphabetically
Emp *salesArray[empSize]; //an array of pointers to pointing to entries by sales

在对它们进行了相同的分配之后,我想使用qsort对其进行不同的排列. 使用struct中的名称和按字母顺序排列的nameArray 使用结构中的总数,将salesArray从最大到最小

after they have been identically assigned, I would like to use a qsort to arrange them differently. the nameArray alphabetically, using the name in the struct and the salesArray largest to smallest, using the total in the struct

compare方法和qsort参数应该是什么样?

What should the compare methods and the qsort arguments look like?

谢谢

推荐答案

您只需要定义两个不同的比较函数即可.每个比较函数都应使用两个指向void的指针(在这种情况下,应将它们强制转换为Emp **类型),然后如果第一个条目小于,等于或等于,则返回负整数,零或正整数.分别大于第二个.

You just need to define two different comparison functions. Each comparison function should take in two pointers to void (in this case, you would cast them to Emp ** types) and then return a negative integer, zero, or a positive integer if the first entry is less than, equal to, or greater than the second one, respectively.

对于基于总数的排序,您可以简单地从第一个中减去第二个total.如果第一个总数小于第二个总数,则结果为负数;如果第一个总数大于第二个总数,则相反.当它们相等时,将返回零.

For the total-based sort, you can simply subtract the second total from the first. If the first total is less than the second, this results in a negative number, and the opposite is true when the first total is greater than the second. When they are equal, zero is returned.

int compareByTotal(const void *first, const void *second)
{
    int firstTotal = (*(Emp **)first)->total;
    int secondTotal = (*(Emp **)second)->total;

    return firstTotal - secondTotal;
}

第二个是字符串比较,可以返回strcmp的值(遵循相同的返回值约定):

The second one, since it's a string comparison, can return the value of a strcmp (which obeys the same return value conventions):

int compareByName(const void *first, const void *second)
{
    const char *firstName = (*(Emp **)first)->name;
    const char *secondName = (*(Emp **)second)->name;

    return strcmp(firstName, secondName);
}

然后,您只需调用qsort并传入这些函数名即可:

Then you can just call qsort passing in those function names:

/* given: */
Emp *nameArray[empSize];//an array of pointers to point to entries alphabetically
Emp *salesArray[empSize]; //an array of pointers to pointing to entries by sales

/* use: */
qsort(nameArray, empSize, sizeof(*nameArray), &compareByName);
qsort(salesArray, empSize, sizeof(*salesArray), &compareByTotal);

这篇关于使用qsort通过不同的变量对结构指针进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆