Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Nov 2004
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    sorting an array of struct by float member

    Ok i'm having touble sorting a member of my struct that is a float w. a qsort.

    void sort_by_cost(concert_record concert[]){
    int i;
    printf("sorting by cost......");
    qsort(concert,RECORD_MAX,sizeof(concert),compare_float);
    for(i=0;i<RECORD_MAX;i++)
    printf("%.2f\n",concert[i].cost);
    }

    int compare_float(const void *x, const void *y){
    return ((concert_record *)x)->cost -
    ((concert_record *)y)->cost;
    }

    i have a similar settup for integers and all i did was replace the integer member in these funtions w/ the float member but its not sorting right.... any ideas?

  • #2
    Regular Coder
    Join Date
    Oct 2004
    Posts
    230
    Thanks
    0
    Thanked 0 Times in 0 Posts
    qsort(concert, RECORD_MAX, sizeof(concert_record), compare_f loat);


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •