GLib.PtrArray.sort
Description: Reference for GLib.PtrArray.sort

### Function

# GLib PtrArraysort

#### Declaration [src]

    void
    g_ptr_array_sort (
      GPtrArray* array,
      GCompareFunc compare_func
    )

#### Description [src]

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 `GCompareFunc` instances, 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 pointer array.

  The data is owned by the caller of the function.

`compare_func`

  Type: `GCompareFunc`

  A comparison function.