Sorting array of structs

Posted by mrblippy on Stack Overflow See other posts from Stack Overflow or by mrblippy
Published on 2010-04-28T01:18:30Z Indexed on 2010/04/28 1:23 UTC
Read the original article Hit count: 239

Filed under:

Hi, i am having trouble making a method to sort an array of structs. i am tring to sort them in ascending order based on classcode. any help you could give would be appreciated

    struct unit
    {
        char classcode[4];
        char *classname;
    };

    void insertion_sort(struct unit u[], int n)
{
    int j, p;
    struct unit tmp[1];
    for(p = 1; p < n; p++)
    {
        tmp[0] = u[p];
        for(j = p; j > 0 && (strcmp(tmp[j-1].classcode, tmp[p].classcode) > 0); j--)
            u[j] = u[j-1];
            u[j] = tmp[0];

    }
}

© Stack Overflow or respective owner

Related posts about c