From 07fcccd8551c22763b6ba8aba2fb3ecd5210df4e Mon Sep 17 00:00:00 2001 From: Zinadin Zidan Date: Sat, 14 Nov 2026 21:58:34 +0600 Subject: [PATCH 1/3] engine: custom-list sort puts unlisted values A to Z after the list (#3949) A custom-list sort key compared only each value's position in the list. Every value outside the list got the same position, so the stable sort left them in their original order. Excel sorts them after the listed values in the usual order. Two unlisted values now compare as a sort without a list does (matchCase included). --- crates/engine/src/cmd/data/sort.rs | 21 ++++++++++++++++----- crates/engine/src/tests.rs | 11 +++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/cmd/data/sort.rs b/crates/engine/src/cmd/data/sort.rs index 491e3d51e..dc1763466 100644 --- a/crates/engine/src/cmd/data/sort.rs +++ b/crates/engine/src/cmd/data/sort.rs @@ -119,15 +119,26 @@ fn do_sort(s: &mut Session, sheet: usize, r: RangeRef, header: bool, keys: Vec a.cmp(&b), + (Some(_), None) => Ordering::Less, + (None, Some(_)) => Ordering::Greater, + // Values not in the list follow the listed ones in the usual order, as in Excel. + (None, None) => plain(), + } } else { - sort_compare(va, vb) + plain() }; // Blanks always sort last, whatever the order. let o = if k.desc && !(va.is_empty() || vb.is_empty()) { o.reverse() } else { o }; diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index f5c17ba16..347190d0e 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -1621,6 +1621,17 @@ fn case_sensitive_sort_puts_lowercase_first() { assert_eq!(col, ["a", "A", "aB", "Ab", "b", "B"].map(Value::from)); } +/// #3949: Excel sorts values that are not in the custom list A to Z after the listed ones. +#[test] +fn custom_list_sort_puts_unlisted_values_after_the_list_in_order() { + let mut s = s(); + s.execute("range.setValues", json!({"range": "A1", "values": [["Priority"], ["Zulu"], ["Alpha"], ["Low"], ["High"], ["Medium"]]})).unwrap(); + let key = json!({"column": "A", "order": "asc", "customList": ["High", "Medium", "Low"]}); + s.execute("data.sort", json!({"range": "A1:A6", "header": true, "keys": [key]})).unwrap(); + let col: Vec = (1..=6).map(|r| v(&s, &format!("A{r}"))).collect(); + assert_eq!(col, ["Priority", "High", "Medium", "Low", "Alpha", "Zulu"].map(Value::from)); +} + #[test] fn data_sort_orders_vowelled_arabic_by_letters() { let mut s = s(); From de3576286e327349e402cb4030c1a5cec0e4e5ad Mon Sep 17 00:00:00 2001 From: Zinadin Zidan Date: Sat, 14 Nov 2026 22:36:26 +0600 Subject: [PATCH 2/3] engine: leave a descending custom-list key as it was (#3949) Comparing two values not in the list changed descending custom-list keys too: they no longer tied, so a later key could not order them. Only an ascending key sorts them now; with a descending one they tie, as before. --- crates/engine/src/cmd/data/sort.rs | 2 ++ crates/engine/src/tests.rs | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/crates/engine/src/cmd/data/sort.rs b/crates/engine/src/cmd/data/sort.rs index dc1763466..b98d9e532 100644 --- a/crates/engine/src/cmd/data/sort.rs +++ b/crates/engine/src/cmd/data/sort.rs @@ -135,6 +135,8 @@ fn do_sort(s: &mut Session, sheet: usize, r: RangeRef, header: bool, keys: Vec Ordering::Less, (None, Some(_)) => Ordering::Greater, // Values not in the list follow the listed ones in the usual order, as in Excel. + // A descending key is left as it was: they tie, so the next key decides. + (None, None) if k.desc => Ordering::Equal, (None, None) => plain(), } } else { diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 347190d0e..dbc6fa536 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -1632,6 +1632,21 @@ fn custom_list_sort_puts_unlisted_values_after_the_list_in_order() { assert_eq!(col, ["Priority", "High", "Medium", "Low", "Alpha", "Zulu"].map(Value::from)); } +/// #3949 changes only an ascending custom-list key: with a descending one, two values not in the +/// list still tie, so the next key orders them. +#[test] +fn descending_custom_list_sort_leaves_unlisted_values_to_the_next_key() { + let mut s = s(); + s.execute("range.setValues", json!({"range": "A1", "values": [["Name", "N"], ["Zulu", 2], ["Alpha", 1], ["High", 3]]})).unwrap(); + let keys = json!([ + {"column": "A", "order": "desc", "customList": ["High", "Medium", "Low"]}, + {"column": "B", "order": "asc"} + ]); + s.execute("data.sort", json!({"range": "A1:B4", "header": true, "keys": keys})).unwrap(); + let col: Vec = (2..=4).map(|r| v(&s, &format!("A{r}"))).collect(); + assert_eq!(col, ["Alpha", "Zulu", "High"].map(Value::from)); +} + #[test] fn data_sort_orders_vowelled_arabic_by_letters() { let mut s = s(); From bece357298b0abe5dc44d12aa5b589db17412baf Mon Sep 17 00:00:00 2001 From: Zinadin Zidan Date: Sat, 14 Nov 2026 22:53:00 +0600 Subject: [PATCH 3/3] engine: a blank goes last in a descending custom-list key (#3949) In a descending custom-list key a blank tied with values not in the list, which come first, while it still went after the listed values, so the order depended on the input. It now goes last. --- crates/engine/src/cmd/data/sort.rs | 3 ++- crates/engine/src/tests.rs | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/cmd/data/sort.rs b/crates/engine/src/cmd/data/sort.rs index b98d9e532..f4adf49ac 100644 --- a/crates/engine/src/cmd/data/sort.rs +++ b/crates/engine/src/cmd/data/sort.rs @@ -136,7 +136,8 @@ fn do_sort(s: &mut Session, sheet: usize, r: RangeRef, header: bool, keys: Vec Ordering::Greater, // Values not in the list follow the listed ones in the usual order, as in Excel. // A descending key is left as it was: they tie, so the next key decides. - (None, None) if k.desc => Ordering::Equal, + // A blank has no place in the list either, but still goes last. + (None, None) if k.desc => va.is_empty().cmp(&vb.is_empty()), (None, None) => plain(), } } else { diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index dbc6fa536..c9953e485 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -1647,6 +1647,18 @@ fn descending_custom_list_sort_leaves_unlisted_values_to_the_next_key() { assert_eq!(col, ["Alpha", "Zulu", "High"].map(Value::from)); } +/// A blank in a descending custom-list key goes last, after the list, rather than tying with the +/// values that are not in the list (which come first), so the order is the same whatever the input. +#[test] +fn descending_custom_list_sort_puts_blanks_last() { + let mut s = s(); + s.execute("range.setValues", json!({"range": "A1", "values": [["Name"], [null], ["High"], ["Zulu"]]})).unwrap(); + let key = json!({"column": "A", "order": "desc", "customList": ["High", "Medium", "Low"]}); + s.execute("data.sort", json!({"range": "A1:A4", "header": true, "keys": [key]})).unwrap(); + let col: Vec = (2..=4).map(|r| v(&s, &format!("A{r}"))).collect(); + assert_eq!(col, [Value::from("Zulu"), Value::from("High"), Value::Empty]); +} + #[test] fn data_sort_orders_vowelled_arabic_by_letters() { let mut s = s();