diff options
author | Stephen Dolan <mu@netsoc.tcd.ie> | 2016-01-18 10:56:54 +0000 |
---|---|---|
committer | Nicolas Williams <nico@cryptonector.com> | 2016-01-18 10:41:25 -0600 |
commit | 7835a724d65708f4511e5d26c5ae9a1a5c1da576 (patch) | |
tree | 5436b25ee5a773539c759a20b14ea0abe1ea9255 | |
parent | 239c357af04366f874a3168d70958409a738fa00 (diff) |
Make jv_sort stable regardless of qsort details.qsort-stability
-rw-r--r-- | src/jv_aux.c | 6 |
1 files changed, 4 insertions, 2 deletions
diff --git a/src/jv_aux.c b/src/jv_aux.c index 17500139..db2e0ef8 100644 --- a/src/jv_aux.c +++ b/src/jv_aux.c @@ -571,14 +571,15 @@ int jv_cmp(jv a, jv b) { struct sort_entry { jv object; jv key; + int index; }; static int sort_cmp(const void* pa, const void* pb) { const struct sort_entry* a = pa; const struct sort_entry* b = pb; int r = jv_cmp(jv_copy(a->key), jv_copy(b->key)); - // comparing by address if r == 0 makes the sort stable - return r ? r : (int)(a - b); + // comparing by index if r == 0 makes the sort stable + return r ? r : (a->index - b->index); } static struct sort_entry* sort_items(jv objects, jv keys) { @@ -590,6 +591,7 @@ static struct sort_entry* sort_items(jv objects, jv keys) { for (int i=0; i<n; i++) { entries[i].object = jv_array_get(jv_copy(objects), i); entries[i].key = jv_array_get(jv_copy(keys), i); + entries[i].index = i; } jv_free(objects); jv_free(keys); |