SDL_qsort_r(3)
π SDL_qsort_r(3) β SDL3 FUNCTIONS β¨
okay bestie, sit down, hydrate, because we're sorting arrays and we're doing it with style. this is a page from the SDL3 FUNCTIONS collection, and today's main character is SDL_qsort_r β the sort that lets you slide a lil userdata pointer into your compare function like a note passed in class. π«Ά
β¨ NAME β¨
SDL_qsort_r β Sort an array, passing a userdata pointer to the compare function.
like, you sort stuff. but your compare function also gets a userdata pointer. that's the whole vibe. no cap. π
π SYNOPSIS π
#include <SDL3/SDL_stdinc.h>
void SDL_qsort_r(void *base, size_t nmemb, size_t size, SDL_CompareCallback_r compare, void *userdata);
π¦ DESCRIPTION π¦
okay so here's a whole example, and honestly? it eats. π
typedef enum {
sort_increasing,
sort_decreasing,
} sort_method;
typedef struct {
int key;
const char *string;
} data;
int SDLCALL compare(const void *userdata, const void *a, const void *b)
{
sort_method method = (sort_method)(uintptr_t)userdata;
const data *A = (const data *)a;
const data *B = (const data *)b;
if (A->key < B->key) {
return (method == sort_increasing) ? -1 : 1;
} else if (B->key < A->key) {
return (method == sort_increasing) ? 1 : -1;
} else {
return 0;
}
}
data values[] = {
{ 3, "third" }, { 1, "first" }, { 2, "second" }
};
SDL_qsort_r(values, SDL_arraysize(values), sizeof(values[0]), compare, (const void *)(uintptr_t)sort_increasing);
see how the userdata gets smuggled into compare and turned back into a sort_method? that's the entire point, bestie. you want increasing? decreasing? your compare function decides, and userdata tells it which way to go. iconic behavior, honestly. π
π FUNCTION PARAMETERS π
baseβ a pointer to the start of the array. the beginning. the origin story. β¨nmembβ the number of elements in the array. how many little guys we're sorting. π’sizeβ the size of the elements in the array. each one's lil footprint. πcompareβ a function used to compare elements in the array. it does the judging (lovingly). πuserdataβ a pointer to pass to the compare function. your secret lil message. π€«
πΈ THREAD SAFETY πΈ
It is safe to call this function from any thread. π§
go off, thread queen. every thread can sort. no gatekeeping here. π
β AVAILABILITY β
This function is available since SDL 3.2.0. so if you're on something olderβ¦ bestie, it's giving β¨upgradeβ¨. π
π SEE ALSO π
the whole friend group, fr fr. π«Ά
and that's the page!! this manual page came to you courtesy of Simple Directmedia Layer, straight from SDL 3.4.18. go sort something beautiful, bestie. ππβ¨
π¦ where this page comes from
- fedora 43: SDL3-devel 3.4.18-1.fc43 Β· Zlib AND MIT AND Apache-2.0 AND (Apache-2.0 OR MIT)
- fedora 43: SDL3-devel 3.4.18-1.fc43 Β· Zlib AND MIT AND Apache-2.0 AND (Apache-2.0 OR MIT)
- fedora 44: SDL3-devel 3.4.18-1.fc44 Β· Zlib AND MIT AND Apache-2.0 AND (Apache-2.0 OR MIT)
- fedora 44: SDL3-devel 3.4.18-1.fc44 Β· Zlib AND MIT AND Apache-2.0 AND (Apache-2.0 OR MIT)
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms