QSORT
NAMESYNOPSIS
DESCRIPTION
RETURN VALUE
VERSIONS
CONFORMING TO
NOTES
EXAMPLE
SEE ALSO
COLOPHON
NAME
qsort, qsort_r − sort an array
SYNOPSIS
#include <stdlib.h>
void
qsort(void *base, size_t nmemb,
size_t size,
int (*compar)(const void *, const void
*));
void
qsort_r(void *base, size_t
nmemb, size_t size,
int (*compar)(const void *, const void *, void
*),
void *arg);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
qsort_r(): _GNU_SOURCE
DESCRIPTION
The qsort() function sorts an array with nmemb elements of size size. The base argument points to the start of the array.
The contents of the array are sorted in ascending order according to a comparison function pointed to by compar, which is called with two arguments that point to the objects being compared.
The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second. If two members compare as equal, their order in the sorted array is undefined.
The qsort_r() function is identical to qsort() except that the comparison function compar takes a third argument. A pointer is passed to the comparison function via arg. In this way, the comparison function does not need to use global variables to pass through arbitrary arguments, and is therefore reentrant and safe to use in threads.
RETURN VALUE
The qsort() and qsort_r() functions return no value.
VERSIONS
qsort_r() was added to glibc in version 2.8.
CONFORMING TO
The qsort() function conforms to SVr4, 4.3BSD, C89, C99.
NOTES
Library routines suitable for use as the compar argument to qsort() include alphasort(3) and versionsort(3). To compare C strings, the comparison function can call strcmp(3), as shown in the example below.
EXAMPLE
For one example of use, see the example under bsearch(3).
Another example is the following program, which sorts the strings given in its command-line arguments:
#include
<stdio.h>
#include <stdlib.h>
#include <string.h>
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(*
(char * const *) p1, * (char * const *) p2);
}
int
main(int argc, char *argv[])
{
int j;
if (argc <
2) {
fprintf(stderr, "Usage: %s <string>...\n",
argv[0]);
exit(EXIT_FAILURE);
}
qsort(&argv[1], argc − 1, sizeof(char *), cmpstringp);
for (j = 1; j
< argc; j++)
puts(argv[j]);
exit(EXIT_SUCCESS);
}
SEE ALSO
sort(1), alphasort(3), strcmp(3), versionsort(3)
COLOPHON
This page is part of release 3.69 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at http://www.kernel.org/doc/man−pages/.
More Linux Commands
manpages/wgetdelay.3ncurses.html
wgetdelay(3ncurses) curses window properties (Man Page).....
This implementation provides functions which return properties set in the WINDOW structure, allowing it to be opaque if the symbol NCURSES_OPAQUE is defined: is
manpages/derwin.3ncurses.html
derwin(3ncurses) - create curses windows - Linux man page...
Calling newwin creates and returns a pointer to a new window with the given number of lines and columns. The upper left-hand corner of the window is at line beg
manpages/XkbResizeKeySyms.3.html
XkbResizeKeySyms(3) - Change the number of symbols bound to
XkbResizeKeySyms reserves the space needed for needed keysyms and returns a pointer to the beginning of the new array that holds the keysyms. It adjusts the off
manpages/gets.3.html
gets(3) - input of characters and strings - Linux man page
Never use this function. gets() reads a line from stdin into the buffer pointed to by s until either a terminating newline or EOF, which it replaces with a null
manpages/mev.1.html
mev(1) - a program to report mouse events - Linux man page
The mev program is part of the gpm package. The information below is extracted from the texinfo file, which is the preferred source of information. The mev prog
manpages/xdg-user-dir.1.html
xdg-user-dir(1) Find an XDG user dir - Linux manual page....
xdg-user-dir looks up the current path for one of the special XDG user dirs. This command expects the name of an XDG user dir as argument. The possible names ar
manpages/rake.1.html
rake(1) Ruby Make (Commands - Linux manual page)............
Rake is a simple ruby(1) build program with capabilities similar to the regular make(1) command. Rake has the following features: * Rakefiles (Rakes version of
manpages/Tcl_GetIntFromObj.3.html
Tcl_GetIntFromObj(3) - manipulate Tcl objects as integer val
These procedures are used to create, modify, and read Tcl objects that &#9474; hold integral values. &#9474; The different routines exist to accommodate differe
manpages/curl.1.html
curl(1) - transfer a URL (Commands - Linux manual page).....
curl is a tool to transfer data from or to a server, using one of the supported protocols (DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS,
manpages/syscall.2.html
syscall(2) - indirect system call - Linux manual page.......
syscall() is a small library function that invokes the system call whose assembly language interface has the specified number with the specified arguments. Empl
manpages/nmcli-examples.5.html
nmcli-examples(5) usage examples of nmcli - Linux man page
nmcli is a command-line client for NetworkManager. It allows controlling NetworkManager and reporting its status. For more information please refer to nmcli(1)
manpages/Tk_FreeCursorFromObj.3.html
Tk_FreeCursorFromObj(3) - maintain database of cursors......
These procedures manage a collection of cursors being used by an application. The procedures allow cursors to be re-used efficiently, thereby avoiding server ov
