1

here i'm again. I had a problem while i was trying to sort an array of struct in c, i know that my code is not good enough but don't be rude with me please!

I tried changing function parameters but i'm exhausted and i'm sure that i'm going to do more errors if i continue, so i need your help.

Here the full code of my program 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.
4
  • 3
    Can you use qsort? Commented Jul 18, 2019 at 16:26
  • I don't know it, how can i use it and how it will work in this case? Commented Jul 18, 2019 at 16:29
  • 1
    I would suggest you to start with reading its documentation Commented Jul 18, 2019 at 16:30
  • Post how ordina_classifica() is called. "sz is the array size" hint that the calling code may be in error. See minimal reproducible example Commented Jul 19, 2019 at 1:11

1 Answer 1

6

Just use the standard function qsort declared in the header <stdlib.h>

But before using it you have to define a function that will compare elements of the array.

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 );
}

and then call qsort like

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

where array is the name of your array of structures, sz is the size of the array.

If you want for example to sort the array by the data member cognome that points to a string then the comparison function can look even simpler

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 );
}

Of course the name of the comparison function may be any you like. So you may define several comparison functions with different names as for example sort_by_td, sort_by_cognome and so on.

Sign up to request clarification or add additional context in comments.

2 Comments

OP's sort attempt is stable. qsort() is not specified to be stable.
@Bjorn A right->td - left->td may overflow (undefined behavior) - sort incorrectly. ( right->td < left->td ) - ( left->td < right->td ) does not have that problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.