diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2019-06-12 14:51:45 -0700 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2019-06-12 14:51:45 -0700 |
| commit | e37f199b87743f7dbb79481d94cf895dbebaf66c (patch) | |
| tree | 6dc3e89acdbb5c6a769267cfa1d04ba79971b695 | |
| parent | 88ed75a56bfebbaa9c5c023ebe2e03664106edbc (diff) | |
Added shortest-first stable sort
| -rw-r--r-- | ask.c | 7 |
1 files changed, 7 insertions, 0 deletions
@@ -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); |
