简体   繁体   English

在结构c的数组内对结构的数组进行排序

[英]sorting an array of struct within array of struct c

I am trying to sort an array of structures by each member of the structure; 我试图按结构的每个成员对结构数组进行排序; ie, I want to print 1 list sorted by each member of the structure. 即,我想打印按结构的每个成员排序的1个列表。 When the members of the structure are integers, no problem. 当结构的成员是整数时,没有问题。 But one of the members is another array of structures, and I also want to sort the whole mess by each member of that structure. 但是其中一个成员是另一个结构数组,我还想按该结构的每个成员对整个混乱进行排序。 Here is the code: 这是代码:

 #define PROPHET_COUNT 9000
 #define MAX_FAITH_COUNT 600

typedef struct s_ProphetStat {  
    int     precursorScore;
    int     cassandraScore;
    int     prophetId;} prophetStat;

typedef struct s_FaithStat{
    int     precursorScore;
    int     cassandraScore;
    int     faithId;
    prophetStat ProphetStat[PROPHET_COUNT];  } faithStat; 

void fauxScoringFunction(faithStat *FaithStat)
{
    for (int faithIndex = 0; faithIndex < MAX_FAITH_COUNT; ++faithIndex){
        for (int prophetIndex = 0; prophetIndex < PROPHET_COUNT; ++prophetIndex){
            int randomNumber = rand();
            FaithStat[faithIndex].ProphetStat[prophetIndex].precursorScore +=   randomNumber;
            FaithStat[faithIndex].ProphetStat[prophetIndex].cassandraScore +=   randomNumber;
            FaithStat[faithIndex].precursorScore += randomNumber;
            FaithStat[faithIndex].cassandraScore += randomNumber; }}
}

typedef int (*compfn)(const void*, const void*);`enter code here`

   int compareFaithPrecursorScores(faithStat *faithA, faithStat *faithB){
 if (faithA->precursorScore > faithB->precursorScore) return 1; if (faithA->precursorScore < faithB->precursorScore) return -1; return 0; }
    int compareFaithCassandraScores(faithStat *faithA, faithStat *faithB) {
  if (faithA->cassandraScore > faithB->cassandraScore) return 1; if (faithA->cassandraScore < faithB->cassandraScore) return -1; return 0; }
    int cannotFigureOut(...) { return 0; }

void fakemain(void)
{
    faithStat   *FaithStat =  (faithStat *)   calloc(MAX_FAITH_COUNT,   sizeof(faithStat) );
    fauxScoringFunction(FaithStat);
    // sort by cumulative precursorScore for each faith 
    qsort(FaithStat, MAX_FAITH_COUNT, sizeof(faithStat *), (compfn) compareFaithPrecursorScores);
    // print results();
    // sort by cumulative precursorScore for each faith
    qsort(FaithStat, MAX_FAITH_COUNT, sizeof(faithStat *), (compfn) compareFaithCassandraScores);
    // print results()
    // sort by prophet precursor score
    qsort(FaithStat, MAX_FAITH_COUNT * PROPHET_COUNT, sizeof(faithStat *), (compfn) cannotFigureOut);
}

It is the "cannotFigureOut()" compare function that I am trying to write. 我尝试编写的是“ cannotFigureOut()”比较函数。 (I am compiling C code using VS2010 C++ (not my decision), thus the nasty calloc cast. All other ugliness is mine.) (我正在使用VS2010 C ++编译C代码(不是我的决定),因此讨厌的calloc cast。其他所有丑陋都是我的。)

Edits: in trying to simplify, botched the compare functions. 编辑:在试图简化时,搞砸了比较功能。 Fixed that. 固定的。 Also, Edit: I omitted an important piece of information: the set of Prophets is the same for each faith. 另外,编辑:我省略了一条重要的信息:先知的集合对于每个信仰都是相同的。 So what I want to do is sort by the cumulative precursor scores (and then, separately, by the cumulative cassandra score) of each prophet. 所以我要做的是按每个先知的累积先驱分数(然后分别由累积卡桑德拉分数)排序。 That is: Prophet[0] cumulativeScore = (Faith[0].Prophet[0].precursorScore + (Faith[1].Prophet[0].precursorScore ... Faith[ MAX_FAITH_COUNT - 1].Prophet[0].precursorScore); 也就是说:先知[0] );

First, return (a,b); 首先, return (a,b); just returns b ; 只返回b ; in both your compareFaithPrecursorScores and compareFaithCassandraScores you probably want to replace , with - . 在这两个compareFaithPrecursorScorescompareFaithCassandraScores你可能要替换,- Now, you only have faiths allocated in your main, so in the last one you should sort the same FaithStat of that same length as before: MAX_FAITH_COUNT , not MAX_FAITH_COUNT * PROPHET_COUNT . 现在,您只在主体中分配了信仰 ,因此在最后一个信仰中,您应该对与之前相同长度的FaithStat进行排序: MAX_FAITH_COUNT ,而不是MAX_FAITH_COUNT * PROPHET_COUNT

Now in your cannotFigureOut , you just compare two faithStats, as before, so the signature is still the same: 现在,在cannotFigureOut ,您只需像以前一样比较两个faithStats,因此签名仍然相同:

int cannotFigureOut(faithStat *faithA, faithStat *faithB){
 .... } 

(edit:) ok, so (after your clarifications) that's not called cumulative at all, that's misleading. (编辑:)好的,所以(在您澄清之后)根本就不称为累积的,这具有误导性。 You meant total score. 你的意思是成绩。 What your last addition seems to say is that you want to sort another array altogether, this time of 9000 prophets (and not of 600 faiths). 您最后添加的内容似乎是要对另一个阵列进行排序,这次是9000个先知(而不是600个信仰)。 Each prophet's score will be the sum of his scores across all faiths; 每个先知的分数将是他所有信仰的分数之和。 just make a function to fill the prophets array up after it's created, then sort as usual. 只需在创建先知数组后创建一个函数,然后照常进行排序即可。

You may use the same prophetStat structure to hold the total scores this time, and not the per-faith values, so that the signature will be 您这次可以使用相同的prophetStat结构来保存总成绩,而不是按信仰值保存,因此签名将是

int cannotFigureOut(prophetStat *prophetA, prophetStat *prophetB){
 .... } 

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

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