【问题标题】:How to sort an array of struct in C?如何在C中对结构数组进行排序?
【发布时间】:2019-11-27 15:39:35
【问题描述】:

我又来了。 我在尝试对 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吗?
  • 我不知道,我该如何使用它以及在这种情况下它将如何工作?
  • 我建议你先阅读它的documentation
  • 发布ordina_classifica() 的调用方式。 “sz 是数组大小”提示调用代码可能有错误。见minimal reproducible example

标签: c arrays sorting struct qsort


【解决方案1】:

只需使用标头&lt;stdlib.h&gt;中声明的标准函数qsort

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

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 等。

【讨论】:

  • OP 的排序尝试是稳定的。 qsort() 未指定为 stable。
  • @Bjorn A right-&gt;td - left-&gt;td 可能会溢出(未定义的行为)- 排序不正确。 ( right-&gt;td &lt; left-&gt;td ) - ( left-&gt;td &lt; right-&gt;td ) 没有这个问题。
猜你喜欢
  • 1970-01-01
  • 2011-05-13
  • 2014-03-12
  • 2016-07-14
  • 1970-01-01
相关资源
最近更新 更多