fzy
terminal fuzzy finder picker
git clone https://9o.is/git/fzy.git
commit 7396d08a074f61cdb0fa7f3d38bd952d22886139 parent 32e5df2da3c2e3478fd228dc3b25a5330c396a5b Author: John Hawthorn <john.hawthorn@gmail.com> Date: Tue, 7 Jun 2016 01:58:41 -0700 Make sorting stable C's stdlib's qsort isn't a stable sort. We want it to be so that any equivalent matches are stored in the order they came in. Diffstat:
| M | src/choices.c | | | 17 | +++++++++++++---- |
1 file changed, 13 insertions(+), 4 deletions(-)
diff --git a/src/choices.c b/src/choices.c @@ -15,12 +15,21 @@ static int cmpchoice(const void *_idx1, const void *_idx2) { const struct scored_result *a = _idx1; const struct scored_result *b = _idx2; - if (a->score == b->score) - return 0; - else if (a->score < b->score) + if (a->score == b->score) { + /* To ensure a stable sort, we must also sort by the string + * pointers. We can do this since we know all the stings are + * from a contiguous memory segment (buffer in choices_t). + */ + if (a->str < b->str) { + return -1; + } else { + return 1; + } + } else if (a->score < b->score) { return 1; - else + } else { return -1; + } } static void *safe_realloc(void *buffer, size_t size) {