hdf_sort_obj(3)

πŸ’– hdf_sort_obj(3) πŸ’–

okay bestie, gather round, we're talking about hdf_sort_obj from util/neo_hdf.h β€” yes it's a section 3 library call, yes it's iconic, no I will not be normal about it ✨


🌸 NAME 🌸

hdf_sort_obj β€” sort the children of an HDF node. that's it. that's the whole job. and it ATE. πŸ’…


πŸŽ€ SYNOPSIS πŸŽ€

#include <util/neo_hdf.h>

hdf_sort_obj()

she's simple, she's clean, she's lowkey mysterious about her parameters (we'll get there, bestie, keep reading 🫢)


πŸ¦‹ ARGUMENTS πŸ¦‹

  • h β€” an HDF node. your lil guy. the one whose children are about to get a glow-up reorder ✨
  • compareFunc β€” a function which returns 1, 0, or -1 depending on some criteria. okay so the tea here is that the arguments to this sort function are pointers to pointers to HDF elements. like, a double pointer. i know. i know. but stay with me. for example:
int sortByName(const void *a, const void *b) {
HDF **ha = (HDF **)a;
HDF **hb = (HDF **)b;
*
return strcasecmp(hdf_obj_name(*ha), hdf_obj_name(*hb));
}
*

see? you cast to HDF **, you deref once, you call hdf_obj_name on each, and strcasecmp does the vibes check for you. so slay. πŸ”₯


🌈 DESCRIPTION 🌈

hdf_sort_obj will sort the children of an HDF node, based on the given comparison function. periodt.

the way it actually works is genuinely kind of a serve: it creates an array of the pointers for each child object of h, uses qsort to sort that array, and then re-orders the linked list of children to match the new order. so like, temporary array era, then linked list glow-up. πŸ’–

now here's the detail you can NOT skip: the qsort compare function uses a pointer to the value in the array, which in our case is a pointer to an HDF struct. so your comparison function should work on HDF ** pointers. double pointer. iykyk. if you write it with a single pointer you're gonna have a bad time, and not the cute kind. 😭


πŸ‘‘ RETURN VALUE πŸ‘‘

None. as in, nothing. zero. void energy. but bestie, the h children will be sorted β€” that's the whole reward, the side effect IS the moment. ✨


🧚 SEE ALSO 🧚

the whole friend group, fr fr:

(yes that last one is missing its (3) β€” i'm not touching it, it's part of the lore πŸ™ˆ)


and that's the page, bestie!! πŸŽ€ this lil baddie is brought to you by ClearSilver, dated 12 July 2007 β€” yes she's vintage, yes she still hits, no notes. go sort something. πŸ’œπŸ¦‹βœ¨