1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date. The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein. The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" References consulted:
24 .\" Linux libc source code
25 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
28 .\" Modified 1993-03-29, David Metcalfe
29 .\" Modified 1993-07-24, Rik Faith (faith@cs.unc.edu)
30 .\" 2006-01-15, mtk, Added example program.
31 .\" Modified 2012-03-08, Mark R. Bannister <cambridge@users.sourceforge.net>
32 .\" and Ben Bacarisse <software@bsb.me.uk>
33 .\" Document qsort_r()
35 .TH QSORT 3 2012-03-08 "" "Linux Programmer's Manual"
37 qsort, qsort_r \- sort an array
40 .B #include <stdlib.h>
42 .BI "void qsort(void *" base ", size_t " nmemb ", size_t " size ,
43 .BI " int (*" compar ")(const void *, const void *));"
45 .BI "void qsort_r(void *" base ", size_t " nmemb ", size_t " size ,
46 .BI " int (*" compar ")(const void *, const void *, void *),"
47 .BI " void *" arg ");"
51 Feature Test Macro Requirements for glibc (see
52 .BR feature_test_macros (7)):
62 function sorts an array with \fInmemb\fP elements of
64 The \fIbase\fP argument points to the start of the
67 The contents of the array are sorted in ascending order according to a
68 comparison function pointed to by \fIcompar\fP, which is called with two
69 arguments that point to the objects being compared.
71 The comparison function must return an integer less than, equal to, or
72 greater than zero if the first argument is considered to be respectively
73 less than, equal to, or greater than the second.
74 If two members compare as equal,
75 their order in the sorted array is undefined.
79 function is identical to
81 except that the comparison function
83 takes a third argument.
84 A pointer is passed to the comparison function via
86 In this way, the comparison function does not need to use global variables to
87 pass through arbitrary arguments, and is therefore reentrant and safe to
94 functions return no value.
97 was added to glibc in version 2.8.
101 function conforms to SVr4, 4.3BSD, C89, C99.
103 Library routines suitable for use as the
111 To compare C strings, the comparison function can call
113 as shown in the example below.
115 For one example of use, see the example under
118 Another example is the following program,
119 which sorts the strings given in its command-line arguments:
127 cmpstringp(const void *p1, const void *p2)
129 /* The actual arguments to this function are "pointers to
130 pointers to char", but strcmp(3) arguments are "pointers
131 to char", hence the following cast plus dereference */
133 return strcmp(* (char * const *) p1, * (char * const *) p2);
137 main(int argc, char *argv[])
142 fprintf(stderr, "Usage: %s <string>...\\n", argv[0]);
146 qsort(&argv[1], argc \- 1, sizeof(char *), cmpstringp);
148 for (j = 1; j < argc; j++)