Last active
August 7, 2022 12:00
-
-
Save outlinepix/3dd881842034ca2379ad60c510fcfbe4 to your computer and use it in GitHub Desktop.
Compare function for qsort() in C language. ( Both Ascending and descending order output)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* IF YOU WANT TO SORT IN DESCENDING ORDER */ | |
int cmp(const void *a, const void *b) | |
{ | |
const int *A = a, *B = b; | |
/* a>b => -1, a<b => 1, a==b => 0 */ | |
return (*A < *B) - (*A > *B); | |
} | |
/* FOR ASCENDING ORDER SORT*/ | |
int cmp(const void *a, const void *b) | |
{ | |
const int *A = a, *B = b; | |
/* a>b => -1, a<b => 1, a==b => 0 */ | |
return (*A > *B) - (*A < *B); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment