Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7557023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:01:04+00:00 2026-05-30T12:01:04+00:00

I am trying to sort an array of structures by each member of the

  • 0

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);

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T12:01:07+00:00Added an answer on May 30, 2026 at 12:01 pm

    First, return (a,b); just returns b; in both your compareFaithPrecursorScores and compareFaithCassandraScores you probably want to replace , with -. 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.

    Now in your cannotFigureOut, you just compare two faithStats, as before, so the signature is still the same:

    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). 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

    int cannotFigureOut(prophetStat *prophetA, prophetStat *prophetB){
     .... } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to sort array with usort, but it doesn't work, What am
For the last 3 days, I have been trying to sort an array, but
I am trying to sort an array of integers in MIPS using bubble sort
I'm trying to sort an array of NSStrings but i'm unsure how to actually
I am trying to sort an array of countries. This way works, but I
I'm trying to sort an array in PHP using a custom function, but it's
I'm trying to sort an array of pointers to structures where the key to
Trying to sort an array by name so I can display an alphabetical list.
I am trying to sort an array of a 2 element data structure alphabetically.
Trying to sort this array of objects according to (1) depth and (2) weight,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.