* than second arg, zero for equal, greater zero if first arg is
* greater than second arg).
*
- * If two array elements compare equal, their order in the sorted array
- * is undefined. If you want equal elements to keep their order (i.e.
- * you want a stable sort) you can write a comparison function that,
- * if two elements would otherwise compare equal, compares them by
- * their addresses.
+ * This is guaranteed to be a stable sort since version 2.32.
**/
void
g_array_sort (GArray *farray,
g_return_if_fail (array != NULL);
- qsort (array->data,
- array->len,
- array->elt_size,
- compare_func);
+ /* Don't use qsort as we want a guaranteed stable sort */
+ g_qsort_with_data (array->data,
+ array->len,
+ array->elt_size,
+ (GCompareDataFunc)compare_func,
+ NULL);
}
/**
*
* Like g_array_sort(), but the comparison function receives an extra
* user data argument.
+ *
+ * This is guaranteed to be a stable sort since version 2.32.
+ *
+ * There used to be a comment here about making the sort stable by
+ * using the addresses of the elements in the comparison function.
+ * This did not actually work, so any such code should be removed.
**/
void
g_array_sort_with_data (GArray *farray,
/**
* g_ptr_array_new_with_free_func:
- * @element_free_func: A function to free elements with destroy @array or %NULL.
+ * @element_free_func: (allow-none): A function to free elements with destroy @array or %NULL.
*
* Creates a new #GPtrArray with a reference count of 1 and use @element_free_func
* for freeing each element when the array is destroyed either via
/**
* g_ptr_array_new_full:
* @reserved_size: number of pointers preallocated.
- * @element_free_func: A function to free elements with destroy @array or %NULL.
+ * @element_free_func: (allow-none): A function to free elements with destroy @array or %NULL.
*
* Creates a new #GPtrArray with @reserved_size pointers preallocated
* and a reference count of 1. This avoids frequent reallocation, if
/**
* g_ptr_array_set_free_func:
* @array: A #GPtrArray.
- * @element_free_func: A function to free elements with destroy @array or %NULL.
+ * @element_free_func: (allow-none): A function to free elements with destroy @array or %NULL.
*
* Sets a function for freeing each element when @array is destroyed
* either via g_ptr_array_unref(), when g_ptr_array_free() is called
* than second arg, zero for equal, greater than zero if irst arg is
* greater than second arg).
*
- * If two array elements compare equal, their order in the sorted array
- * is undefined. If you want equal elements to keep their order (i.e.
- * you want a stable sort) you can write a comparison function that,
- * if two elements would otherwise compare equal, compares them by
- * their addresses.
- *
* <note><para>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.</para></note>
+ *
+ * This is guaranteed to be a stable sort since version 2.32.
**/
void
g_ptr_array_sort (GPtrArray *array,
{
g_return_if_fail (array != NULL);
- qsort (array->pdata,
- array->len,
- sizeof (gpointer),
- compare_func);
+ /* Don't use qsort as we want a guaranteed stable sort */
+ g_qsort_with_data (array->pdata,
+ array->len,
+ sizeof (gpointer),
+ (GCompareDataFunc)compare_func,
+ NULL);
}
/**
* <note><para>The comparison function for g_ptr_array_sort_with_data()
* doesn't take the pointers from the array as arguments, it takes
* pointers to the pointers in the array.</para></note>
+ *
+ * This is guaranteed to be a stable sort since version 2.32.
**/
void
g_ptr_array_sort_with_data (GPtrArray *array,