add bsearch for arrays

Arrays can already be sorted with json_object_array_sort() which uses
qsort() of the standard C library. This adds a counterpart using the
bsearch() from C.
This commit is contained in:
Alexander Dahl
2014-08-21 15:42:50 +02:00
parent d4e81f9ec8
commit 2f5789bdef
4 changed files with 49 additions and 2 deletions

View File

@@ -91,8 +91,14 @@ array_list_add(struct array_list *arr, void *data)
void
array_list_sort(struct array_list *arr, int(*sort_fn)(const void *, const void *))
{
qsort(arr->array, arr->length, sizeof(arr->array[0]),
(int (*)(const void *, const void *))sort_fn);
qsort(arr->array, arr->length, sizeof(arr->array[0]), sort_fn);
}
void* array_list_bsearch( const void **key, struct array_list *arr,
int (*sort_fn)(const void *, const void *) )
{
return bsearch( key, arr->array, arr->length, sizeof(arr->array[0]),
sort_fn );
}
int