Next: rand, Previous: on_exit, Up: Stdlib [Contents][Index]

### 2.32 `qsort`—sort an array

Synopsis

    #include <stdlib.h>
    void qsort(void *base, size_t nmemb, size_t size,
        int (*compar)(const void *, const void *) );

Description
`qsort` sorts an array (beginning at base) of nmemb objects. size describes the size of each element of the array.

You must supply a pointer to a comparison function, using the argument shown as compar. (This permits sorting objects of unknown properties.) Define the comparison function to accept two arguments, each a pointer to an element of the array starting at base. The result of `(*compar)` must be negative if the first argument is less than the second, zero if the two arguments match, and positive if the first argument is greater than the second (where “less than” and “greater than” refer to whatever arbitrary ordering is appropriate).

The array is sorted in place; that is, when `qsort` returns, the array elements beginning at base have been reordered.

Returns
`qsort` does not return a result.

Portability
`qsort` is required by ANSI (without specifying the sorting algorithm).