Function

GLibsort_array

since: 2.82

Declaration [src]

void
g_sort_array (
  void* array,
  size_t n_elements,
  size_t element_size,
  GCompareDataFunc compare_func,
  void* user_data
)

Description [src]

This is just like the standard C qsort() function, but the comparison routine accepts a user data argument (like qsort_r()).

Unlike qsort(), this is guaranteed to be a stable sort.

An example of sorting an array of strings (a strv):

static int
strcmp_data (const void *a,
             const void *b,
             void       *user_data)
{
  return strcmp (*((const char * const *) a), *((const char * const *) b));
}

char **my_strv = …;
size_t my_strv_length = g_strv_length (my_strv);
g_sort_array (my_strv, (my_strv_length, sizeof (*my_strv), strcmp_data, NULL);

Available since: 2.82

This function is not directly available to language bindings.

Parameters

array

Type: An array of void

Start of array to sort.

The length of the array is specified in the n_elements argument.
The data is owned by the caller of the function.
n_elements

Type: size_t

Number of elements in the array.

element_size

Type: size_t

Size of each element.

compare_func

Type: GCompareDataFunc

Function to compare elements.

user_data

Type: void*

Data to pass to compare_func.

The argument can be NULL.
The data is owned by the caller of the function.