aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2019-06-12 14:51:45 -0700
committerBruce Hill <bruce@bruce-hill.com>2019-06-12 14:51:45 -0700
commite37f199b87743f7dbb79481d94cf895dbebaf66c (patch)
tree6dc3e89acdbb5c6a769267cfa1d04ba79971b695
parent88ed75a56bfebbaa9c5c023ebe2e03664106edbc (diff)
Added shortest-first stable sort
-rw-r--r--ask.c7
1 files changed, 7 insertions, 0 deletions
diff --git a/ask.c b/ask.c
index 7ea4de5..ba83315 100644
--- a/ask.c
+++ b/ask.c
@@ -295,6 +295,11 @@ static char *get_input(FILE *in, FILE *out, const char *prompt, const char *init
return picked;
}
+static int cmp_len(const void *a, const void *b)
+{
+ return (int)(strlen(*(char**)a) - strlen(*(char**)b));
+}
+
int main(int argc, char *argv[])
{
int yes = 0, no = 0;
@@ -397,6 +402,8 @@ int main(int argc, char *argv[])
if (tcsetattr(fileno(tty_out), TCSAFLUSH, &bb_termios) == -1)
return 1;
+ // Prefer shorter matches, but otherwise keep original order (mergesort is stable)
+ mergesort(opts, (size_t)nopts, sizeof(char*), cmp_len);
char *output = get_input(tty_in, tty_out, prompt, query, nopts, opts);
fflush(tty_out);