Я пытаюсь отсортировать следующую структуру:
typedef struct thing {
char *text;
int count;
} *Item;
Я создаю дополнительный вектор для сортировки структур следующим образом:
Item items[nr_of_items];
... here is a loop to place the items inside the vector items ...
qsort(items, nr_of_items, sizeof(Item), cmpItem);
А вот и функция сравнения:
int cmpItem(const void *a, const void *b) {
const Item item_a = (const Item)a;
const Item item_b = (const Item)b;
/* Return 1 or -1 if members are not equal */
if (item_a->count > item_b->count) return 1;
if (item_a->count < item_b->count) return -1;
/* Return 1 or -1 if members are not equal */
if ( strcmp ( item_a->text, item_b->text ) > 0 ) return 1;
if ( strcmp ( item_a->text, item_b->text ) < 0 ) return -1;
/* Return 0 if both members are equal in both structures */
return 0;
}
Вектор создается правильно, но не сортируется. Я делаю что-то неправильно? Любая помощь приветствуется.
const Item item_a = *(const Item *)a
. - person Steve Summit   schedule 10.05.2016Item
задан как указатель, если все указатели в исходном массивеitems
настроены правильно. Действительно лиitems[]
содержитnr_of_items
допустимых указателей наstruct thing
? (а как насчет выделения указателейtext
, вы уверены, что и с ними все в порядке?) - person Steve Summit   schedule 10.05.2016