编辑代码

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
// using namespace std;

typedef struct Node
{
    char * rune;
    int val;
}node;

//字符比较
int cmp_str(const void*a,const void *b)
{
    return strcmp((char*)a,(char*)b);
}

//数字比较
int cmp(const void*a,const void *b)
{
    return *(int *)a>*(int*)b ?1:-1;
}

//为结构体,复合数据结构提供的比较,以node为例
int cmp_compare(const void*a,const void *b)
{
    node *a_= (node*)a;
    node *b_= (node*)b;


    if (  strcmp(a_->rune,b_->rune) < 0    )
        return -1;
    else if (  strcmp(a_->rune,b_->rune) > 0    )
        return  1;
    else
    return a_->val-b_->val ;
}

void PRINT_ELEM(char **a,int n)
{
   for(int i=0;i<n;i++)
   {
    //    cout<<*(a+i)<<endl;
    printf("%s\n",*(a+i));
   }
}

int main()
{
    char *a[5]={"abd","ddi","dda","aba","byd"};
    // qsort(a,sizeof(a)/sizeof(a[0]),sizeof(a[0]),cmp_str);
    // PRINT_ELEM(a,sizeof(a)/sizeof(a[0]));

    int b[5]={6,6,6,8,6};
    // qsort(b,5,4,cmp);
    // for(int i=0;i<5;i++)printf("%d ",b[i]);


    node c[5];
    for(int i=0;i<5;i++)
    {
        c[i].val=b[i];c[i].rune=a[i];
        printf("%s  ||  %d\n",c[i].rune,c[i].val);
    }

    qsort(c,5,sizeof(c[0]),cmp_compare);
    for(int i=0;i<5;i++)
    {
        
        printf("%s  ||  %d\n",c[i].rune,c[i].val);
    }

}