diff options
author | Alexander Dahl <post@lespocky.de> | 2014-08-21 15:42:50 +0200 |
---|---|---|
committer | Alexander Dahl <post@lespocky.de> | 2014-08-21 15:42:50 +0200 |
commit | 2f5789bdef5c65b951db1add50b08028b0051c36 (patch) | |
tree | 4cb30a8ce95dbac3f9f7dcefa5159ec7728695c5 /arraylist.c | |
parent | d4e81f9ec8273914739808737fa0a27a3f0589fb (diff) | |
download | json-c-2f5789bdef5c65b951db1add50b08028b0051c36.tar.gz |
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.
Diffstat (limited to 'arraylist.c')
-rw-r--r-- | arraylist.c | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/arraylist.c b/arraylist.c index 1d899fa..5ccbdd2 100644 --- a/arraylist.c +++ b/arraylist.c @@ -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 |