Merge pull request #4182 from ZIDAN44/fix/3949-custom-list-unlisted-sort

engine: custom-list sort puts unlisted values A to Z after the list (#3949)
This commit is contained in:
FredApps authored and GitHub committed 2026-11-16 10:21:26 +01:00
commit e1147239fc
2 files changed
+57 -5

No files matched your search

+19 -5
View File
@@ -119,15 +119,29 @@ fn do_sort(s: &mut Session, sheet: usize, r: RangeRef, header: bool, keys: Vec<S
for (ki, k) in keys.iter().enumerate() {
let (va, ca) = &keyed[a as usize][ki];
let (vb, cb) = &keyed[b as usize][ki];
let plain = || {
if case && va.is_text() && vb.is_text() {
sort_compare(va, vb).then_with(|| case_order(va.as_text().unwrap_or(""), vb.as_text().unwrap_or("")))
} else {
sort_compare(va, vb)
}
};
let o = if k.by != "values" {
ca.cmp(cb)
} else if let Some(list) = &k.list {
let pos = |v: &Value| v.as_text().and_then(|t| list.iter().position(|x| x.eq_ignore_ascii_case(t))).unwrap_or(usize::MAX);
pos(va).cmp(&pos(vb))
} else if case && va.is_text() && vb.is_text() {
sort_compare(va, vb).then_with(|| case_order(va.as_text().unwrap_or(""), vb.as_text().unwrap_or("")))
let pos = |v: &Value| v.as_text().and_then(|t| list.iter().position(|x| x.eq_ignore_ascii_case(t)));
match (pos(va), pos(vb)) {
(Some(a), Some(b)) => 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.
// A descending key is left as it was: they tie, so the next key decides.
// 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 {
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 };
+38
View File
@@ -1621,6 +1621,44 @@ 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<Value> = (1..=6).map(|r| v(&s, &format!("A{r}"))).collect();
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<Value> = (2..=4).map(|r| v(&s, &format!("A{r}"))).collect();
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<Value> = (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();