Function

GLibPtrArraysort

Declaration

void
g_ptr_array_sort (
  GPtrArray* array,
  GCompareFunc compare_func
)

Description

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 first 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.

Use g_ptr_array_sort_values() if you want to use normal GCompareFuncs, otherwise here is a full example of use:

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.

This function is not directly available to language bindings.

Parameters

array

Type: An array of gpointer

A GPtrArray.

The data is owned by the caller of the function.
compare_func

Type: GCompareFunc

Comparison function.