c++ - qsort/compare
- Paul (30/30) Mar 18 2007 Hello, I need to sort an array of structures into desending order
- Bertel Brander (20/28) Mar 18 2007 int compare_one(const void *v1, const void *v2)
- Paul (3/3) Mar 18 2007 Hi Bertel, thanks your code works fine.
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
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
Hi Bertel, thanks your code works fine. Kind regards Paul
Mar 18 2007