c - 如何在 C 中对结构数组进行排序?

标签 c arrays sorting struct qsort

我又来了。 我在尝试对 C 中的结构数组进行排序时遇到问题,我知道我的代码不够好,但请不要对我无礼!

我尝试更改函数参数,但我已经筋疲力尽了,我确信如果我继续,我会犯更多的错误,所以我需要你的帮助。

这是我程序的完整代码 https://pastebin.com/p28EbY8i

// I've 2 struct 

typedef struct{     // Not used in this function
    int id;
    char * nome;
    char * presidente;
    char * allenatore;
} squadra;

typedef struct{     // I've an array of this type of data
        int id;
        char * nome;
        char * cognome;
        int eta;
        char * ruolo;
        squadra team;
        char * college;
        int td;
    } giocatore;

// This is what i wrote for my function

size_t ordina_classifica(size_t sz, giocatore array[]){  //sz is the array 
                                                         //size
    giocatore temp;
    for(size_t i = 0; i < sz; ++i){
        for(size_t j = i + 1; j < sz; ++j){
            if(array[i].td > array[j].td){

                temp.id = array[i].id;
                temp.nome = array[i].nome;
                temp.cognome = array[i].cognome;
                temp.eta = array[i].eta;
                temp.ruolo = array[i].ruolo;
                temp.team.nome = array[i].team.nome;
                temp.college = array[i].college;
                temp.td = array[i].td;

                array[i].id = array[j].id;
                array[i].nome = array[j].nome;
                array[i].cognome = array[j].cognome;
                array[i].eta = array[j].eta;
                array[i].ruolo = array[j].ruolo;
                array[i].team.nome = array[j].team.nome;
                array[i].college = array[j].college;
                array[i].td = array[j].td;

                array[j].id = temp.id;
                array[j].nome = temp.nome;
                array[j].cognome = temp.cognome;
                array[j].eta = temp.eta;
                array[j].ruolo = temp.ruolo;
                array[j].team.nome = temp.team.nome;
                array[j].college = temp.college;
                array[j].td = temp.td;
            }
        }
    }
    return 2;  // I need this for the rest of the code (in case of sort 
                   // success)
}

//I need to sort my array by the data 'td' that is a integer but for now my //compiler don't print errors, but only crash when i try to select this //function.

最佳答案

只需使用标准函数qsort在 header 中声明 <stdlib.h>

但在使用它之前,您必须定义一个函数来比较数组的元素。

int cmp( const void *a, const void *b )
{
    const giocatore *left  = ( const giocatore * )a;
    const giocatore *right = ( const giocatore * )b;

    return ( right->td < left->td ) - ( left->td < right->td );
}

然后像这样调用qsort

qsort( array, sz, sizeof( giocatore ), cmp );

哪里array是结构数组的名称,sz是数组的大小。

例如,如果您想按数据成员 cognome 对数组进行排序指向一个字符串然后比较函数看起来更简单

int cmp( const void *a, const void *b )
{
    const giocatore *left  = ( const giocatore * )a;
    const giocatore *right = ( const giocatore * )b;

    return strcmp( left->cognome, right->cognome );
}

当然比较函数的名字可以随便取。因此,您可以定义多个具有不同名称的比较函数,例如 sort_by_td , sort_by_cognome等等。

关于c - 如何在 C 中对结构数组进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57098974/

相关文章:

java - 如何有效地按组对列表进行排序?

c - 定义结构函数

c - 使用自相关进行基音检测

algorithm - “Finding Maximum Sum of Subsequent Elements”算法分析

python - 在 Python 中使用新轴

使用数据表对图像进行排序

c++ - std::sort 什么时候停止比较

c - 使用 memcpy 获取段错误

c - GNU make 不包含头文件

c++ - 将固定长度的括号括起来的初始化程序列表传递给函数