To sort an array of strings in ascending order, I use:
int cmp(const void *p, const void *q) {
char* const *pp = p;
char* const *qq = q;
return strcmp(*pp, *qq);
}
This will be then implemented into a qsort like so:
qsort(a, sizeof(a)(sizeof(a[0]), sizeof(a[0]), cmp);
How do you sort it in descending order?
One quick and easy way to do this is to multiply the result of strcmp() by -1 before returning it.
int cmp(const void *p, const void *q) {
char* const *pp = p;
char* const *qq = q;
return -strcmp(*pp, *qq);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With