SDL_qsort_r

Sort an array, passing a userdata pointer to the compare function.

extern (C) nothrow @nogc extern
void
SDL_qsort_r
(
void* base
,
size_t nmemb
,
size_t size
,,
void* userdata
)

Examples

enum sort_method {
                sort_increasing,
                sort_decreasing,
            }

            struct data {
                int key;
                const(char)* value;
            }

            int compare(sort_method method, const(data)* a, const(data)* b) {
                if (a.key < b.key) {
                    return (method == sort_method.sort_increasing) ? -1 : 1;
                } else if (b.key < a.key) {
                    return (method == sort_method.sort_increasing) ? 1 : -1;
                } else {
                    return 0;
                }
            }

            data[] values = [
                { 3, "third" }, { 1, "first" }, { 2, "second" }
            ];

            // reinterpret_cast is a part of numem.
            SDL_qsort_r(values.ptr, values.length, data.sizeof, cast(SDL_CompareCallback_r)&compare, reinterpret_cast!(void*)(sort_method.sort_increasing));
        

\param base a pointer to the start of the array. \param nmemb the number of elements in the array. \param size the size of the elements in the array. \param compare a function used to compare elements in the array. \param userdata a pointer to pass to the compare function.

\threadsafety It is safe to call this function from any thread.

\since This function is available since SDL 3.2.0.

\sa SDL_bsearch_r \sa SDL_qsort

Meta