SDL_CompareCallback_r(3type)

πŸ’– SDL_CompareCallback_r πŸ’–

okay bestie, so this is a page from the SDL3 DATATYPES corner of the manual β€” it's SDL_CompareCallback_r(3type), and it's literally the callback you hand over to SDL's sorting and binary search functions so they know how to compare your stuff. no cap, it's giving "tiny function, huge responsibility." 🫢


✨ NAME ✨

SDL_CompareCallback_r β€” a callback used with SDL sorting and binary search functions. that's it, that's the whole job, and she does it flawlessly πŸ’…


🌸 SYNOPSIS 🌸

#include <SDL3/SDL_stdinc.h>

typedef int (SDLCALL *SDL_CompareCallback_r)(void *userdata, const void *a, const void *b);

look at that signature. so clean. so iconic. πŸŽ€


πŸ¦‹ FUNCTION PARAMETERS πŸ¦‹

  • userdata β€” the userdata pointer that got passed to the sort function. it just gets handed right back to you, like a little "hey, here's your stuff" moment πŸ™ˆ
  • a β€” a pointer to the first element being compared.
  • b β€” a pointer to the second element being compared.

two elements walk into a callback… and you're the one who decides who goes first 😭


⭐ RETURN VALUE ⭐

she returns:

  • -1 if a should be sorted before b
  • 1 if b should be sorted before a
  • 0 if they are equal
⚠️ heads up bestie: if two elements are equal, their order in the sorted array is undefined. so don't be out here relying on equal elements keeping their lil original spots β€” that's not a thing. undefined is undefined, periodt. πŸ’€

🌈 AVAILABILITY 🌈

this callback has been available since SDL 3.2.0. so if you're on anything older… no she's not there yet, sorry babe 🧚


πŸ‘‘ SEE ALSO πŸ‘‘

go read those next, they're the ones actually calling you. iykyk πŸ”₯


and that's the page, bestie!! this one came from Simple Directmedia Layer, SDL 3.4.18, and it's SDL_CompareCallback_r(3type) β€” mwah, slay, go write a comparator that actually returns the right int πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from