qsort的cmp函数
日期:2015-06-04 10:35:55
最后更新日期:2017-10-07 16:40:15
1.为负,表示a排在b前面
2.为正,表示a排在b后面
3.为0,表示a排前排后无所谓
思考:
对含有两个成员的node结构体,若实现按l的从小到大排序,l相等,则按r从大到小排序
[code lang="cpp"]
struct node{
double l,r;
};
int cmp(void *a,void *b){
struct node *pa=(struct node*)a;
struct node *pb=(struct node*)b;
if (pa->l-pb->l<0){
return -1;
}
if (pa->l-pb->l>0){
return 1;
}
if (pa->r-pb->r>0){
return -1;
}
//开始没加下面这句
if (pa->r-pb->r<0){
return 1;
}
return 0;
}
[/code]