Я пытаюсь сортировать массив структур по каждому члену структуры; т.е. я хочу, чтобы напечатал 1 список, отсортированный по каждому члену структуры. Когда члены структуры являются целыми числами, не проблема. Но один из членов - это еще один массив структур, , и я также хочу сортировать весь беспорядок каждым членом этой структуры. Вот код:сортировка массива структуры внутри массива struct c
#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);
}
Это функция «cannotFigureOut()» сравнить, что я пытаюсь писать. (Я компилирую C-код, используя VS2010 C++ (не мое решение), таким образом, противный calloc-литой. Все остальное уродство принадлежит мне.)
Редактирование: в попытках упростить, испортить функции сравнения. Исправлено. Кроме того, Редактировать: я пропустил важную часть информации: множество пророков одинаково для каждой веры. Итак, что я хочу сделать, это сортировать по суммарно прекурсоров (а затем отдельно по совокупному счету кассандры) каждого пророка. То есть: Пророк [0] cumulativeScore = (Faith [0] .Prophet [0] .precursorScore + (Вера [1] .Prophet [0] .precursorScore ... Вера [MAX_FAITH_COUNT - 1] .Prophet [0]. precursorScore);
Ваш вопрос не ясен. Вы хотите, чтобы иметь возможность сортировать, например. 'faithStat.prohetStat [0] .precursorScore' и на' faithStat.prohetStat [0] .cassandraScore' и на 'faithStat.prohetStat [1] .precursorScore' и т. д.? –
Да, последнее: каждая вера дает разные оценки каждому из пророков, но все они имеют один и тот же список пророков. – PaeneInsula