commit f92b7a2e1ac819ed3291b834dbf1b6a008c2873d
parent ee0ca19605dec0939977957207dda9af5eb997e3
Author: Louis Burda <quent.burda@gmail.com>
Date: Mon, 28 Feb 2022 12:10:41 +0100
Fix sorted insert
Diffstat:
1 file changed, 9 insertions(+), 4 deletions(-)
diff --git a/src/list.c b/src/list.c
@@ -116,14 +116,19 @@ list_insert_sorted(struct list *list, struct link *insert,
{
struct link *link;
- ASSERT(list != NULL && link != NULL && compare != NULL);
+ ASSERT(list != NULL && insert != NULL && compare != NULL);
+
+ /* Simple Insertion Sort */
+ /* cmp(a,b) -> (a-b) */
for (LIST_ITER(list, link)) {
- if (compare(insert, link) > 0) {
- link_append(link, insert);
- break;
+ if (compare(link, insert) >= 0) {
+ link_prepend(link, insert);
+ return;
}
}
+
+ list_push_back(list, insert);
}
void