C语言的qsort函数可以实现先按从大到小然后若相等再从小到大吗?

struct In
{
int x;
int y;
}s[100];

//按照x从小到大排序,当x相等时按照y从大到小排序
int cmp( const void *a , const void *b )
{
struct In *c = (struct In *)a;
struct In *d = (struct In *)b;
if(c->x != d->x) return c->x - d->x;
else return d->y - c->y;
}
qsort(s,100,sizeof(s[0]),cmp);
C语言的qsort函数可以实现先按从大到小然后若相等再从小到大吗?比如这个,可以实现吗?

可以。测试代码:

#include <stdio.h>
#include <stdlib.h> 

struct In
{
int x;
int y;
};

//按照x从小到大排序,当x相等时按照y从大到小排序
int cmp( const void *a , const void *b )
{
struct In *c = (struct In *)a;
struct In *d = (struct In *)b;
if(c->x != d->x) return c->x - d->x;
else return d->y - c->y;
}


int main(void){
    struct In nums[5] = {
        {1,2},
        {2,3},
        {4,1},
        {4,2},
        {4,3}
    };
    
    int i,j; 
    printf("排序前:\n");
    for(i=0;i<5;i++){
        printf("%d %d\n",nums[i].x,nums[i].y);
    }
    
    
    qsort(nums,5,sizeof(nums[0]),cmp);
    printf("排序后:\n");
        for(i=0;i<5;i++){
        printf("%d %d\n",nums[i].x,nums[i].y);
    }
    
}

img