From 905d799f527ef60470c0e80e41b720d9b265b90f Mon Sep 17 00:00:00 2001 From: Robby Zambito Date: Sun, 3 Oct 2021 21:42:49 -0400 Subject: Sort indices after parsing them. When we use the indices we assume they are sorted. This makes sure they are sorted, rather than assuming the user entered them in order. --- src/todo.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/todo.c b/src/todo.c index 88def16..c7f416f 100644 --- a/src/todo.c +++ b/src/todo.c @@ -135,6 +135,18 @@ void todo_add(int argc, char **argv) { fclose(todo_file); } +static int int_compare(const void *a, const void *b) { + int int_a = *((int *)a); + int int_b = *((int *)b); + + if (int_a == int_b) + return 0; + else if (int_a < int_b) + return -1; + else + return 1; +} + static size_t *parse_indices(int argc, char **argv) { size_t *res = calloc(sizeof(size_t), argc); @@ -147,6 +159,7 @@ static size_t *parse_indices(int argc, char **argv) { } } + qsort(res, argc, sizeof(size_t), int_compare); return res; } -- cgit v1.2.3