简体   繁体   English

如何在 C 中按降序对结构数组进行排序

[英]How to sort array of structs by descending order in C

These are my structures:这些是我的结构:

struct create{
    char names[30];
    int Win;
    int Lose;
    int Draw;
    int Points;
    int Average;
    int Goals;
};

So, I was searching for a method on how to sort them, by descending order.所以,我正在寻找一种如何按降序对它们进行排序的方法。 Like, according to int Win if Win is higher than other than it should be at the top.就像,根据int Win如果 Win 高于其他值,它应该在顶部。

For ex:例如:

Name - Win - Lose - Draw ...
Joe  -  2  -   0  -  0 ...
Bill -  1  -   0  -  1 ...
Mike -  0  -   1  -  1 ...

Is it possible?可能吗? Can someone help me with this.有人可以帮我弄这个吗。

Use the standard C function qsort declared in the header <stdlib.h> creating an appropriate comparison function. Use the standard C function qsort declared in the header <stdlib.h> creating an appropriate comparison function.

Here you are.给你。

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

struct create{
    char names[30];
    int Win;
    int Lose;
    int Draw;
    int Points;
    int Average;
    int Goals;
};

int cmp( const void *a, const void *b )
{
    const struct create *left  = a;
    const struct create *right = b;

    return ( left->Win < right->Win ) - ( right->Win < left->Win );  
}

int main(void) 
{
    struct create a[] =
    {
        { .names = "Mike", .Win = 0, .Lose = 1, .Draw = 1 },
        { .names = "Joe",  .Win = 2, .Lose = 0, .Draw = 0 },
        { .names = "Bill", .Win = 1, .Lose = 0, .Draw = 1 },
    };

    const size_t N = sizeof( a ) / sizeof( *a );

    for ( size_t i = 0; i < N; i++ )
    {
        printf( "%-4s %d %d %d\n", a[i].names, a[i].Win, a[i].Lose, a[i].Draw );
    }

    putchar( '\n' );

    qsort( a, N, sizeof( struct create ), cmp );

    for ( size_t i = 0; i < N; i++ )
    {
        printf( "%-4s %d %d %d\n", a[i].names, a[i].Win, a[i].Lose, a[i].Draw );
    }

    putchar( '\n' );

    return 0;
}

The program output is程序 output 是

Mike 0 1 1
Joe  2 0 0
Bill 1 0 1

Joe  2 0 0
Bill 1 0 1
Mike 0 1 1

Oh man... you don't specify anything in a precise way.,,.哦,伙计...您没有以精确的方式指定任何内容。,,。 You show almost no code, no complete example, no working code.您几乎没有显示代码,没有完整的示例,没有工作代码。 Please, read this page to get on how to post in StackOverflow.请阅读此页面以了解如何在 StackOverflow 中发帖。 I'll assume you are using qsort() to sort the array in ascending order, with some function called comparator1() , and assume that the function is defined as:我假设您正在使用qsort()对数组进行升序排序,其中一些 function 称为 compare1 comparator1() ,并假设 function 定义为:

int comparator1(void *a, void *b);

to compare structs in order to sort in ascending order.比较结构以便按升序排序。 Just write this another function:只需编写另一个 function:

int comparator2(void *a, void *b) 
{
    return -comparator1(a, b);
}

and pass this second comparator function to the call to qsort() .并将第二个比较器 function 传递给对qsort()的调用。 You'll get the array sorted in the opposite order than before.您将按照与以前相反的顺序对数组进行排序。 This way, you can control the order to be ascending or descending by just selecting one function or the other.这样,您只需选择一个 function 或另一个即可控制顺序是升序还是降序。 :) :)

A typical sort looks something like:典型的排序类似于:

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

struct create {
        char name[30];
        int Win;
};

int
comp(const void *va, const void *vb)
{
        const struct create *a = va;
        const struct create *b = vb;
        return a->Win > b->Win ? 1 : b->Win > a->Win ? -1 : 0;
}

int
main(void)
{
        struct create ar[] = {
                { .name = "a", .Win = 5 },
                { .name = "b", .Win = 7 },
                { .name = "c", .Win = 3 },
                { .name = "d", .Win = -1 },
                { .name = "e", .Win = 5 },
                { .name = "f", .Win = 8 }
        };
        qsort(ar, sizeof ar / sizeof *ar, sizeof *ar, comp);
        for( unsigned i = 0; i < sizeof ar / sizeof *ar; i++ ) {
                printf(" %s: %d\n", ar[i].name, ar[i].Win);
        }

        return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM