C语言 对结构体数组的成员进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13372688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:25:01  来源:igfitidea点击:

sorting members of structure array

carrayssortingstructure

提问by bardockyo

Given a structure array (in C) I am attempting to print out the results in groups of gender and in sub order by numerical order. For example:

给定一个结构数组(在 C 中),我试图按性别和按数字顺序按子顺序打印结果。例如:

struct employee{
char gender[13]
char name[13];
int id;
};

Say I define the structure array like so:

假设我像这样定义结构数组:

struct employee info[2]={{"male","Matt",1234},{"female","Jessica",2345},{"male","Josh",1235}};

How could I go about printing the results like

我怎么能去打印这样的结果

1234 Matt
1235 Josh


2345 Jessica

回答by kallikak

You'll need to implement a sorting function that compares the structs as you require

您需要实现一个排序函数来根据需要比较结构

int compare(const void *s1, const void *s2)
{
  struct employee *e1 = (struct employee *)s1;
  struct employee *e2 = (struct employee *)s2;
  int gendercompare = strcmp(e1->gender, e2->gender);
  if (gendercompare == 0)  /* same gender so sort by id */
    return e1->id - e2->id;
  else
    return -gendercompare;  /* the minus puts "male" first as in the question */
}

And then use qsort from the standard library.

然后使用标准库中的 qsort。

qsort(data, count, sizeof(struct employee), compare);

Inside the compare function you may want to check for id being equal, then you can sort by name (also using strcmp()) however you like.

在 compare 函数中,您可能想要检查 id 是否相等,然后您可以按名称排序(也可以使用strcmp())。

Ken

Edit: Just compiled and fixed this up. Here's a little test program

编辑:刚刚编译并修复了这个问题。这是一个小测试程序

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

    struct employee{
      char gender[13];
      char name[13];
      int id;
    };

    int compare(const void *s1, const void *s2)
    {
      struct employee *e1 = (struct employee *)s1;
      struct employee *e2 = (struct employee *)s2;
      int gendercompare = strcmp(e1->gender, e2->gender);
      if (gendercompare == 0)  /* same gender so sort by id */
        return e1->id - e2->id;
      else
        return -gendercompare;
    }

    main()
    {
      int i;
      struct employee info[]={{"male","Matt",1234},{"female","Jessica",2345},{"male","Josh",1235}};

      for (i = 0; i < 3; ++i)
        printf("%d\t%s\t%s\n", info[i].id, info[i].gender, info[i].name);

      qsort(info, 3, sizeof(struct employee), compare);

      for (i = 0; i < 3; ++i)
        printf("%d\t%s\t%s\n", info[i].id, info[i].gender, info[i].name);
    }

With output:

有输出:

$ ./a.exe
1234    male    Matt
2345    female  Jessica
1235    male    Josh
1234    male    Matt
1235    male    Josh
2345    female  Jessica

回答by 1''

Use your favourite sorting algorithm on the struct array. When comparing two elements of the array to decide which is "greater", compare their genders; if the genders are the same, compare their numbers. (You may want to define a separate function to do this comparison, to make things clearer.) Afterwards, print the sorted array in order using the desired formatting. Keep track of when the gender switches from male to female so you can put in an extra three newlines, as in your example.

在 struct 数组上使用您最喜欢的排序算法。在比较数组的两个元素以确定哪个“更大”时,比较它们的性别;如果性别相同,比较他们的数字。(您可能需要定义一个单独的函数来进行此比较,以使事情更清晰。)然后,使用所需的格式按顺序打印已排序的数组。跟踪性别何时从男性变为女性,以便您可以添加额外的三个换行符,如您的示例所示。

Edit: to borrow shamelessly from kallikak, you can just pass your comparison function to qsort, but have it return 1 if one struct is "greater", -1 if it is "less" and (if necessary) 0 if it is the same (using the procedure I outlined above). Take a look at How to write a compare function for qsort from stdlib?for help on writing a custom compare function.

编辑:无耻地从 kallikak 借用,您可以将比较函数传递给 qsort,但如果一个结构“更大”,则返回 1,如果“更少”则返回 -1,如果相同则返回 0(如有必要) (使用我上面概述的程序)。看看如何从 stdlib 为 qsort 编写比较函数?有关编写自定义比较函数的帮助。

回答by Progga Ilma

Think it's easier to understand,as I am weak in pointer ,hope it helps............

认为它更容易理解,因为我在指针方面很弱,希望它有所帮助......

#include<bits/stdc++.h>

using namespace std;


struct employee{
  char gender[13];
  char name[13];
  int id;
};

bool compare(employee s1,employee s2)
{
  return s1.id<s2.id;
}

main()
{
  int i;
  struct employee info[]={{"male","Matt",1234},{"female","Jessica",2345},{"male","Josh",1235}};
  sort(info,info+3,compare);
  for (i = 0; i < 3; i++)
  printf("%d\t%s\t%s\n",info[i].id,info[i].gender,info[i].name);
}