www.digitalmars.com         C & C++   DMDScript  

c++.dos.32-bits - Qsort

reply Paul <Paul_member pathlink.com> writes:
Hi Walter, Im trying to sort an array in
desending order.  compare_weight is working ok, 
however compare_sp is not.  I understood that all 
I needed to do was reverse the return values to 
accomplish this. However, im finding that sometimes 
the first two values in the sorted array are not in 
descending order. Ive posted both compare functions below.

As always your help appreciated. Paul

/* WORKS OK, passed pointers to integer values */
int compare_weight(const void *v1, const void *v2)
{
int return_value;

return_value = (*(struct runners*)v1).weight - (*(struct runners*)v2).weight;

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

/* ERRORS IN SORETED ARRAY, passed poiners to float values */
int compare_sp(const void *v1, const void *v2)
{
int return_value;

return_value = (*(struct runners*)v1).sp - (*(struct runners*)v2).sp;

if(return_value < 0 )
{
return -34;
}
else if( return_value > 0 )
{
return 28;
}
else
{
return 0;
}
return return_value;
}
Nov 26 2003
parent "Walter" <walter digitalmars.com> writes:
I would suggest that the .weight and .sp members are not both the same sort
order (though you use them as if they were in the two sorts).

"Paul" <Paul_member pathlink.com> wrote in message
news:bq3afr$1ebl$1 digitaldaemon.com...
 Hi Walter, Im trying to sort an array in
 desending order.  compare_weight is working ok,
 however compare_sp is not.  I understood that all
 I needed to do was reverse the return values to
 accomplish this. However, im finding that sometimes
 the first two values in the sorted array are not in
 descending order. Ive posted both compare functions below.

 As always your help appreciated. Paul

 /* WORKS OK, passed pointers to integer values */
 int compare_weight(const void *v1, const void *v2)
 {
 int return_value;

 return_value = (*(struct runners*)v1).weight - (*(struct
runners*)v2).weight;
 if(return_value < 0 )
 {
 return 28;
 }
 else if( return_value > 0 )
 {
 return -34;
 }
 else
 {
 return 0;
 }
 return return_value;
 }

 /* ERRORS IN SORETED ARRAY, passed poiners to float values */
 int compare_sp(const void *v1, const void *v2)
 {
 int return_value;

 return_value = (*(struct runners*)v1).sp - (*(struct runners*)v2).sp;

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