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. 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. (I am compiling C code using VS2010 C++ (not my decision), thus the nasty calloc cast. All other ugliness is mine.)
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);
First,
return (a,b);just returnsb; in both yourcompareFaithPrecursorScoresandcompareFaithCassandraScoresyou probably want to replace,with-. Now, you only have faiths allocated in your main, so in the last one you should sort the sameFaithStatof that same length as before:MAX_FAITH_COUNT, notMAX_FAITH_COUNT * PROPHET_COUNT.Now in your
cannotFigureOut, you just compare two faithStats, as before, so the signature is still the same:(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). 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
prophetStatstructure to hold the total scores this time, and not the per-faith values, so that the signature will be