Hi,
I've an array with some values and NaNs.
How can I use qsort that NaN entries are moved to the end of the sorted array ?
Thanks!
You need to create a function that compares the floats and returns a specific value depending on your criteria.
How would you normally compare for a NAN ?
John
A way to do it:
int compare(const void *a, const void *b)
{
float arg1 = *((float *)a);
float arg2 = *((float *)b);
if ( isnan(arg1) && isnan(arg2) )
{
return 0;
}
else if ( isnan(arg1) )
{
return 1;
}
else if ( isnan(arg2) )
{
return -1;
}
else if ( arg1 < arg2 )
{
return -1;
}
else if ( arg1 == arg2 )
{
return 0;
}
else
{
return 1;
}
}
Ok, fine. If that works for you.
John