Commit 0e23d48c8b4752bb31b2584ffa37ed4497078777

Thomas de Grivel 2024-11-09T15:46:20

fix list_sort_by (List.sort_by(list, fn))

diff --git a/ikc3/.ikc3_history b/ikc3/.ikc3_history
index 4367de1..c6577d0 100644
--- a/ikc3/.ikc3_history
+++ b/ikc3/.ikc3_history
@@ -1,6 +1,3 @@
-quote fn (x) { x * x }
-quote if true do if false do 1 else fn (x) { x * x } end end
-quote if true do if false do 1 else fn (x) do x * x end end end
 quote if true do if false do %KC3.Operator{sym: :-, symbol_value: 3} else fn (x) do x * x end end end
 if true do if true do %KC3.Operator{sym: :-, symbol_value: 3} else fn (x) do x * x end end end
 type(0)
@@ -97,3 +94,6 @@ Struct.put(%KC3.Operator{}, :sym, :plop)
 a = %Time{}
 a.tv_sec
 a.tv_nsec
+List.sort_by([1, 2, 3, 4], fn (a, b) { a > b })
+List.sort_by([%{title: "Abc", time: %Time{}}, %{title: "Cde", time: %Time{tv_sec: 1000}}], fn (a, b) { a.time < b.time })
+List.sort_by([%{title: "Abc", time: %Time{}}, %{title: "Cde", time: %Time{tv_sec: 1000}}], fn (a, b) { a.time > b.time })
diff --git a/libkc3/list.c b/libkc3/list.c
index f1ecb85..4ec2ab1 100644
--- a/libkc3/list.c
+++ b/libkc3/list.c
@@ -356,13 +356,15 @@ s_list ** list_sort_by (const s_list * const *list, const s_fn *compare,
   l = *list;
   while (l) {
     t = &tmp;
-    if (! tag_init_copy(&arg1->tag, &(*t)->tag))
-      goto ko;
-    if (! tag_init_copy(&arg2->tag, &l->tag))
-      goto ko;
     while (*t) {
+      if (! tag_init_copy(&arg1->tag, &(*t)->tag))
+        goto ko;
+      if (! tag_init_copy(&arg2->tag, &l->tag))
+        goto ko;
       if (! eval_fn_call(compare, arg1, &tag))
         goto ko;
+      tag_void(&arg1->tag);
+      tag_void(&arg2->tag);
       if (! bool_init_cast(&b, &sym_Bool, &tag)) {
         tag_clean(&tag);
         goto ko;