sdlQsortR function

void sdlQsortR(
  1. Pointer<NativeType> base,
  2. int nmemb,
  3. int size,
  4. Pointer<NativeFunction<SdlCompareCallbackR>> compare,
  5. Pointer<NativeType> userdata,
)

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

For example:

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);

\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.1.3.

\sa SDL_bsearch_r \sa SDL_qsort

extern SDL_DECLSPEC void SDLCALL SDL_qsort_r(void *base, size_t nmemb, size_t size, SDL_CompareCallback_r compare, void *userdata)

Implementation

void sdlQsortR(
    Pointer<NativeType> base,
    int nmemb,
    int size,
    Pointer<NativeFunction<SdlCompareCallbackR>> compare,
    Pointer<NativeType> userdata) {
  final sdlQsortRLookupFunction = libSdl3.lookupFunction<
      Void Function(
          Pointer<NativeType> base,
          Uint32 nmemb,
          Uint32 size,
          Pointer<NativeFunction<SdlCompareCallbackR>> compare,
          Pointer<NativeType> userdata),
      void Function(
          Pointer<NativeType> base,
          int nmemb,
          int size,
          Pointer<NativeFunction<SdlCompareCallbackR>> compare,
          Pointer<NativeType> userdata)>('SDL_qsort_r');
  return sdlQsortRLookupFunction(base, nmemb, size, compare, userdata);
}