I tried to sort an array of Products by its unit price but the result doesn't work.
typedef struct {
int supply;
int totalPrice;
double unitPrice;
} Product;
int comparator(const void * a, const void * b) {
return (*(Product*) b).unitPrice - (*(Product*) a).unitPrice;
}
int main() {
Product m[3] = {
{18, 75, 4.17},
{15, 72, 4.80},
{10, 45, 4.50}
};
qsort(m, 3, sizeof(Product), comparator);
for (int i = 0; i < 3; i++) {
printf("unitPrice=%f\n", m[i].unitPrice);
}
}