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();