T
t_pantel
I 've got the following structure:
typedef struct GROUPED
{
short val ;
short code;
short group;
short forecast_cd;
short double_ind;
short min;
short max;
} GROUPED;
and an define array:
GROUPED test[30];
A possible example for the values tha this structure array could
contain are:
test[0].val=1
test[1].val=1
test[2].val=2
test[3].val=3
test[0].code=111
test[1].code=112
test[2].code=113
test[2].code=114
Suppose that the remainung fields are 0.
I want to sort the results by "val" and I am trying to use qsort() to
achieve this.
My comparison function is:
int compare( const void *arg1, const void *arg2 )
{
if (((GROUPED *)arg1)->val==0)
return 1;
if (((GROUPED *)arg2)->val==0)
return -1;
if (((GROUPED *)arg1)->val > ((GROUPED*)arg2)->val)
return (1);
return (-1);
}
and I call qsort like this:
qsort(match, (size_t)30, sizeof(GROUPED), compare);
The result after calling qsort is this:
test[0].val=1
test[1].val=1
test[2].val=2
test[3].val=3
test[0].code=112
test[1].code=111
test[2].code=113
test[2].code=114
As it is obvious it reverses test[0].code and test[1].code values that
is, the values of the .code that have the same .val value!
What am I doing wrong?
Thanks a lot.
Thanos
typedef struct GROUPED
{
short val ;
short code;
short group;
short forecast_cd;
short double_ind;
short min;
short max;
} GROUPED;
and an define array:
GROUPED test[30];
A possible example for the values tha this structure array could
contain are:
test[0].val=1
test[1].val=1
test[2].val=2
test[3].val=3
test[0].code=111
test[1].code=112
test[2].code=113
test[2].code=114
Suppose that the remainung fields are 0.
I want to sort the results by "val" and I am trying to use qsort() to
achieve this.
My comparison function is:
int compare( const void *arg1, const void *arg2 )
{
if (((GROUPED *)arg1)->val==0)
return 1;
if (((GROUPED *)arg2)->val==0)
return -1;
if (((GROUPED *)arg1)->val > ((GROUPED*)arg2)->val)
return (1);
return (-1);
}
and I call qsort like this:
qsort(match, (size_t)30, sizeof(GROUPED), compare);
The result after calling qsort is this:
test[0].val=1
test[1].val=1
test[2].val=2
test[3].val=3
test[0].code=112
test[1].code=111
test[2].code=113
test[2].code=114
As it is obvious it reverses test[0].code and test[1].code values that
is, the values of the .code that have the same .val value!
What am I doing wrong?
Thanks a lot.
Thanos