1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" References consulted:
26 .\" Linux libc source code
27 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
30 .\" Modified 1993-03-29, David Metcalfe
31 .\" Modified 1993-07-24, Rik Faith (faith@cs.unc.edu)
32 .\" 2006-01-15, mtk, Added example program.
33 .\" Modified 2012-03-08, Mark R. Bannister <cambridge@users.sourceforge.net>
34 .\" and Ben Bacarisse <software@bsb.me.uk>
35 .\" Document qsort_r()
37 .TH QSORT 3 2021-03-22 "" "Linux Programmer's Manual"
39 qsort, qsort_r \- sort an array
42 .B #include <stdlib.h>
44 .BI "void qsort(void *" base ", size_t " nmemb ", size_t " size ,
45 .BI " int (*" compar ")(const void *, const void *));"
46 .BI "void qsort_r(void *" base ", size_t " nmemb ", size_t " size ,
47 .BI " int (*" compar ")(const void *, const void *, void *),"
48 .BI " void *" arg ");"
52 Feature Test Macro Requirements for glibc (see
53 .BR feature_test_macros (7)):
63 function sorts an array with \fInmemb\fP elements of
65 The \fIbase\fP argument points to the start of the
68 The contents of the array are sorted in ascending order according to a
69 comparison function pointed to by \fIcompar\fP, which is called with two
70 arguments that point to the objects being compared.
72 The comparison function must return an integer less than, equal to, or
73 greater than zero if the first argument is considered to be respectively
74 less than, equal to, or greater than the second.
75 If two members compare as equal,
76 their order in the sorted array is undefined.
80 function is identical to
82 except that the comparison function
84 takes a third argument.
85 A pointer is passed to the comparison function via
87 In this way, the comparison function does not need to use global variables to
88 pass through arbitrary arguments, and is therefore reentrant and safe to
95 functions return no value.
98 was added to glibc in version 2.8.
100 For an explanation of the terms used in this section, see
108 Interface Attribute Value
112 T} Thread safety MT-Safe
119 POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.
121 To compare C strings, the comparison function can call
123 as shown in the example below.
125 For one example of use, see the example under
128 Another example is the following program,
129 which sorts the strings given in its command-line arguments:
137 cmpstringp(const void *p1, const void *p2)
139 /* The actual arguments to this function are "pointers to
140 pointers to char", but strcmp(3) arguments are "pointers
141 to char", hence the following cast plus dereference. */
143 return strcmp(*(const char **) p1, *(const char **) p2);
147 main(int argc, char *argv[])
150 fprintf(stderr, "Usage: %s <string>...\en", argv[0]);
154 qsort(&argv[1], argc \- 1, sizeof(char *), cmpstringp);
156 for (int j = 1; j < argc; j++)