PtrArray.sort

Sorts the array, using @compare_func which should be a qsort()-style comparison function (returns less than zero for first arg is less than second arg, zero for equal, greater than zero if irst arg is greater than second arg).

Note that the comparison function for g_ptr_array_sort() doesn't take the pointers from the array as arguments, it takes pointers to the pointers in the array. Here is a full example of usage:

|[<!-- language="C" --> typedef struct { gchar *name; gint size; } FileListEntry;

static gint sort_filelist (gconstpointer a, gconstpointer b) { const FileListEntry *entry1 = *((FileListEntry **) a); const FileListEntry *entry2 = *((FileListEntry **) b);

return g_ascii_strcasecmp (entry1->name, entry2->name); }

… g_autoptr (GPtrArray) file_list = NULL;

// initialize file_list array and load with many FileListEntry entries ... // now sort it with g_ptr_array_sort (file_list, sort_filelist); ]|

This is guaranteed to be a stable sort since version 2.32.

class PtrArray
void
sort
(
GCompareFunc compareFunc
)

Parameters

compareFunc GCompareFunc

comparison function

Meta