Skip to content

Commit

Permalink
Use qsort for alphabetical sorting
Browse files Browse the repository at this point in the history
Signed-off-by: Aaron Ayub <[email protected]>
  • Loading branch information
aaronayub authored and artemsen committed Feb 10, 2024
1 parent f983354 commit 5abec66
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/imagelist.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,19 +269,25 @@ static size_t peek_edge(bool first)
return peek_next_file(ctx.index, first);
}

/**
* Compare paths with strcoll
* @return negative if a < b, positive if a > b, 0 otherwise
*/
static int compare_paths(const void* a, const void* b)
{
const struct entry* entry_a = *(const struct entry**)a;
const struct entry* entry_b = *(const struct entry**)b;

return strcoll(entry_a->path, entry_b->path);
}

/**
* Sort the image list alphabetically.
*/
static void sort_list(void)
{
qsort(ctx.entries, ctx.size, sizeof(struct entry*), compare_paths);
for (size_t i = 0; i < ctx.size; ++i) {
for (size_t j = i + 1; j < ctx.size; ++j) {
if (strcoll(ctx.entries[i]->path, ctx.entries[j]->path) > 0) {
struct entry* swap = ctx.entries[i];
ctx.entries[i] = ctx.entries[j];
ctx.entries[j] = swap;
}
}
ctx.entries[i]->index = i;
}
}
Expand Down

0 comments on commit 5abec66

Please sign in to comment.