diff options
author | Bram Moolenaar <Bram@vim.org> | 2014-07-02 19:06:18 +0200 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2014-07-02 19:06:18 +0200 |
commit | c35e3de8cc0786609fd7975416d22e7193f7187d (patch) | |
tree | 3f0fc44ee14a7fc21ce73e7cc449d50a78148a4a /src/eval.c | |
parent | 81439a6d1b45bbef9c8dc03d76f01b6a8165e2ce (diff) |
updated for version 7.4.351v7.4.351
Problem: sort() is not stable.
Solution: When the items are identical, compare the pointers.
Diffstat (limited to 'src/eval.c')
-rw-r--r-- | src/eval.c | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/src/eval.c b/src/eval.c index 2aa4e5407a..84a798c7cc 100644 --- a/src/eval.c +++ b/src/eval.c @@ -17334,6 +17334,7 @@ static int item_compare_numeric; static char_u *item_compare_func; static dict_T *item_compare_selfdict; static int item_compare_func_err; +static int item_compare_keep_zero; static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort)); #define ITEM_COMPARE_FAIL 999 @@ -17374,6 +17375,12 @@ item_compare(s1, s2) n2 = strtod((char *)p2, (char **)&p2); res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1; } + + /* When the result would be zero, compare the pointers themselves. Makes + * the sort stable. */ + if (res == 0 && !item_compare_keep_zero) + res = s1 > s2 ? 1 : -1; + vim_free(tofree1); vim_free(tofree2); return res; @@ -17396,7 +17403,7 @@ item_compare2(s1, s2) if (item_compare_func_err) return 0; - /* copy the values. This is needed to be able to set v_lock to VAR_FIXED + /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED * in the copy without changing the original list items. */ copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]); copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]); @@ -17415,6 +17422,12 @@ item_compare2(s1, s2) if (item_compare_func_err) res = ITEM_COMPARE_FAIL; /* return value has wrong type */ clear_tv(&rettv); + + /* When the result would be zero, compare the pointers themselves. Makes + * the sort stable. */ + if (res == 0 && !item_compare_keep_zero) + res = s1 > s2 ? 1 : -1; + return res; } @@ -17509,6 +17522,7 @@ do_sort_uniq(argvars, rettv, sort) ptrs[i++] = li; item_compare_func_err = FALSE; + item_compare_keep_zero = FALSE; /* test the compare function */ if (item_compare_func != NULL && item_compare2((void *)&ptrs[0], (void *)&ptrs[1]) @@ -17536,6 +17550,7 @@ do_sort_uniq(argvars, rettv, sort) /* f_uniq(): ptrs will be a stack of items to remove */ item_compare_func_err = FALSE; + item_compare_keep_zero = TRUE; item_compare_func_ptr = item_compare_func ? item_compare2 : item_compare; |