www.digitalmars.com         C & C++   DMDScript  

c++ - qsort/compare

reply Paul <wcount12 aol.com> writes:
Hello, I need to sort an array of structures into desending order
by an element declared as a float.  Here is my compare function
which im using with Qsort.
----------------------------------------------------------------
int compare_one(const void *v1, const void *v2)
{
    int return_value;

    return_value = (*(struct proform*)v1).eq_rating - (*(struct
proform*)v2).eq_rating;

    if(return_value < 0 )
    {
       return 28;
    }
    else if( return_value > 0 )
    {
       return -34;
    }
    else
    {
       return 0;
    }

    return return_value;
}
-----------------------------------------------------------

This is sorting as if the variables are int's rather than floats,
so the frational part is being ignored.

Question:  How do sort on a float variable this is part of a
structure?

Thanks

Paul
Mar 18 2007
parent reply Bertel Brander <bertel post4.tele.dk> writes:
Paul skrev:
 Hello, I need to sort an array of structures into desending order
 by an element declared as a float.  Here is my compare function
 which im using with Qsort.
[Snip code]
 This is sorting as if the variables are int's rather than floats,
 so the frational part is being ignored.
 
 Question:  How do sort on a float variable this is part of a
 structure?
int compare_one(const void *v1, const void *v2) { double return_value = (*(struct proform*)v1).eq_rating - (*(struct proform*)v2).eq_rating; if(return_value < 0 ) { return 1; } else if( return_value > 0 ) { return -1; } return 0; } -- Just another homepage: http://damb.dk But it's mine - Bertel
Mar 18 2007
parent Paul <wcount12 aol.com> writes:
Hi Bertel, thanks your code works fine.

Kind regards

Paul
Mar 18 2007