qsort(3) Library Functions Manual qsort(3) qsort, qsort_r - C (libc, -lc) #include void qsort(void base[.size * .nmemb], size_t nmemb, size_t size, int (*compar)(const void [.size], const void [.size])); void qsort_r(void base[.size * .nmemb], size_t nmemb, size_t size, int (*compar)(const void [.size], const void [.size], void *), void *arg); glibc (. feature_test_macros(7)): qsort_r(): _GNU_SOURCE qsort() nmemb size. base . , compar, , . , , , , . , . qsort_r() qsort(), , compar . arg. , , . qsort() qsort_r() . attributes(7). +----------------------------+----------------------------------------------------------+--------------------------+ | | | | +----------------------------+----------------------------------------------------------+--------------------------+ |qsort(), qsort_r() | | MT-Safe | +----------------------------+----------------------------------------------------------+--------------------------+ qsort() C11, POSIX.1-2008. qsort() POSIX.1-2001, C89, SVr4, 4.3BSD. qsort_r() glibc 2.8. C- strcmp(3), . , bsearch(3). , , : #include #include #include static int cmpstringp(const void *p1, const void *p2) { /* The actual arguments to this function are "pointers to pointers to char", but strcmp(3) arguments are "pointers to char", hence the following cast plus dereference. */ return strcmp(*(const char **) p1, *(const char **) p2); } int main(int argc, char *argv[]) { if (argc < 2) { fprintf(stderr, "Usage: %s ...\n", argv[0]); exit(EXIT_FAILURE); } qsort(&argv[1], argc - 1, sizeof(char *), cmpstringp); for (size_t j = 1; j < argc; j++) puts(argv[j]); exit(EXIT_SUCCESS); } sort(1), alphasort(3), strcmp(3), versionsort(3) () Artyom Kunyov , Azamat Hackimov , Konstantin Shvaykovskiy Yuri Kozlov ; GNU (GNU General Public License - GPL, 3 ) , - . - , , () () () <>. Linux 6.9.1 15 2024 . qsort(3)