From a8893b94e67a438c314792d2b49b6febcf16a333 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Tue, 1 Dec 2026 07:48:19 -0500 Subject: [PATCH 1/3] fix(table): sort an attribute table column in one total order --- crates/ui-egui/src/table.rs | 77 ++++++++++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/table.rs b/crates/ui-egui/src/table.rs index 3afee6024c..63c992c724 100644 --- a/crates/ui-egui/src/table.rs +++ b/crates/ui-egui/src/table.rs @@ -10,6 +10,7 @@ use std::sync::Weak; use egui::{Sense, Stroke, Ui, vec2}; use egui_extras::{Column, TableBuilder}; +use rustgis_engine::data::value::cmp_values; use rustgis_engine::data::{Dataset, Feature, Field, Value as V}; use serde_json::{Value, json}; @@ -86,7 +87,8 @@ impl RowOrder { } if let Some((c, asc)) = sort { indices.sort_by(|a, b| { - let o = ds.value(*a, c).partial_cmp(ds.value(*b, c)).unwrap_or(std::cmp::Ordering::Equal); + // Not `Value`'s `partial_cmp`: that isn't a total order (#3483). + let o = cmp_values(ds.value(*a, c), ds.value(*b, c)); if asc { o } else { o.reverse() } }); } @@ -1168,6 +1170,79 @@ mod tests { assert!(h.query_by_label("Aardvark parcel").is_some()); } + /// The values of a one-column table in the order the table lists them. + fn row_order(values: &[V], ascending: bool, filter: &str, selection: Option<&[usize]>) -> Vec { + let mut ds = Dataset::new("Values", rustgis_engine::data::GeometryType::Point, fields(&["v"])); + for v in values { + ds.push(None, vec![v.clone()]); + } + let mut cache = None; + let rows = RowOrder::displayed(&mut cache, &ds, Some((0, ascending)), filter, selection).unwrap_or_default(); + rows.iter().map(|&r| format!("{:?}", ds.value(r, 0))).collect() + } + + fn shown(values: &[V]) -> Vec { + values.iter().map(|v| format!("{v:?}")).collect() + } + + #[test] + fn row_order_puts_negative_infinity_first() { + let values = [-4.5, 3.5, -0.5, 10.5, 7.5, f64::NEG_INFINITY].map(V::Float); + let mut want = shown(&[f64::NEG_INFINITY, -4.5, -0.5, 3.5, 7.5, 10.5].map(V::Float)); + assert_eq!(row_order(&values, true, "", None), want); + want.reverse(); + assert_eq!(row_order(&values, false, "", None), want); + } + + #[test] + fn row_order_puts_infinity_and_nan_after_the_numbers() { + let values = [V::Float(f64::NAN), V::Float(f64::INFINITY), V::Float(20.0), V::Null, V::Float(-3.0)]; + let want = shown(&[V::Null, V::Float(-3.0), V::Float(20.0), V::Float(f64::INFINITY), V::Float(f64::NAN)]); + assert_eq!(row_order(&values, true, "", None), want); + } + + /// The same order however the rows came, whatever mix of kinds the column holds. + #[test] + fn row_order_of_mixed_kinds_does_not_depend_on_how_the_rows_came() { + let date = rustgis_engine::data::Date::parse("2026-12-01").unwrap(); + let want = [ + V::Null, + V::Bool(true), + V::Int(9), + V::Float(9.5), + V::Date(date), + V::Text("10".into()), + V::Text("A".into()), + V::Text("a".into()), + ]; + let expected = shown(&want); + // Every permutation, by Heap's algorithm. + let mut values = want.to_vec(); + let mut counts = vec![0; values.len()]; + let mut i = 1; + assert_eq!(row_order(&values, true, "", None), expected); + while i < values.len() { + if counts[i] < i { + values.swap(if i % 2 == 0 { 0 } else { counts[i] }, i); + assert_eq!(row_order(&values, true, "", None), expected, "rows {values:?}"); + counts[i] += 1; + i = 1; + } else { + counts[i] = 0; + i += 1; + } + } + } + + #[test] + fn row_order_sorts_only_the_rows_a_filter_or_the_selection_keeps() { + let values = [-4.5, 3.5, -0.5, f64::NEG_INFINITY].map(V::Float); + assert_eq!(row_order(&values, true, ".5", None), shown(&[-4.5, -0.5, 3.5].map(V::Float))); + assert_eq!(row_order(&values, true, "", Some(&[2, 3])), shown(&[f64::NEG_INFINITY, -0.5].map(V::Float))); + assert_eq!(row_order(&values[..1], true, "", None), shown(&values[..1])); + assert!(row_order(&[], true, "", None).is_empty()); + } + /// A value typed into a cell is kept on Enter, and Escape leaves the cell as it was (not the /// tool: the shortcuts run first, egui has dropped the focus on Escape by then). The editor /// took the focus back every frame, so Enter never ended it and nothing was kept (#1182). From 1e0e78248fb898676891787de3bc823998d8fa7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Tue, 1 Dec 2026 08:05:04 -0500 Subject: [PATCH 2/3] test(table): check a mixed column descending too --- crates/ui-egui/src/table.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/table.rs b/crates/ui-egui/src/table.rs index 63c992c724..1afafa1a1f 100644 --- a/crates/ui-egui/src/table.rs +++ b/crates/ui-egui/src/table.rs @@ -87,7 +87,7 @@ impl RowOrder { } if let Some((c, asc)) = sort { indices.sort_by(|a, b| { - // Not `Value`'s `partial_cmp`: that isn't a total order (#3483). + // Not `Value`'s `partial_cmp`, which isn't a total order: see `cmp_values`. let o = cmp_values(ds.value(*a, c), ds.value(*b, c)); if asc { o } else { o.reverse() } }); @@ -1232,6 +1232,8 @@ mod tests { i += 1; } } + let reversed: Vec<_> = expected.iter().rev().cloned().collect(); + assert_eq!(row_order(&values, false, "", None), reversed); } #[test] From 78b55c88bafeeb9881441c371a917c03479f72ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Tue, 1 Dec 2026 08:18:19 -0500 Subject: [PATCH 3/3] perf(data): tie equal dates without building their texts --- crates/data/src/value.rs | 17 ++++++++++++++++- crates/ui-egui/src/table.rs | 3 ++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/crates/data/src/value.rs b/crates/data/src/value.rs index 21a5dd846a..a664553dcb 100644 --- a/crates/data/src/value.rs +++ b/crates/data/src/value.rs @@ -430,7 +430,10 @@ pub fn cmp_values(x: &Value, y: &Value) -> std::cmp::Ordering { _ => 1, }; rank(x).cmp(&rank(y)).then_with(|| match (x, y) { - (Value::Date(_) | Value::DateTime(_), _) => x.temporal_key().cmp(&y.temporal_key()).then_with(|| x.to_string().cmp(&y.to_string())), + // Equal values tie without building their texts: a column often repeats its dates. + (Value::Date(_) | Value::DateTime(_), _) => { + x.temporal_key().cmp(&y.temporal_key()).then_with(|| if x == y { Ordering::Equal } else { x.to_string().cmp(&y.to_string()) }) + } (Value::Text(a), Value::Text(b)) => { a.chars().flat_map(char::to_lowercase).cmp(b.chars().flat_map(char::to_lowercase)).then_with(|| a.cmp(b)) } @@ -785,6 +788,18 @@ mod tests { assert_eq!(std::mem::size_of::(), 24); } + /// Equal dates tie; a date and a date-time at its midnight keep one order, the date first. + #[test] + fn dates_at_one_instant_keep_one_order() { + use std::cmp::Ordering; + let d = Value::parse("2026-03-05", FieldType::Date); + let t = Value::parse("2026-03-05T00:00:00", FieldType::DateTime); + assert_eq!(cmp_values(&d, &d.clone()), Ordering::Equal); + assert_eq!(cmp_values(&t, &t.clone()), Ordering::Equal); + assert_eq!(cmp_values(&d, &t), Ordering::Less); + assert_eq!(cmp_values(&t, &d), Ordering::Greater); + } + #[test] fn dates_compare_with_dates_and_date_text() { let d = Value::parse("2026-03-05", FieldType::Date); diff --git a/crates/ui-egui/src/table.rs b/crates/ui-egui/src/table.rs index 1afafa1a1f..da037a5aa4 100644 --- a/crates/ui-egui/src/table.rs +++ b/crates/ui-egui/src/table.rs @@ -1177,10 +1177,11 @@ mod tests { ds.push(None, vec![v.clone()]); } let mut cache = None; - let rows = RowOrder::displayed(&mut cache, &ds, Some((0, ascending)), filter, selection).unwrap_or_default(); + let rows = RowOrder::displayed(&mut cache, &ds, Some((0, ascending)), filter, selection).unwrap(); rows.iter().map(|&r| format!("{:?}", ds.value(r, 0))).collect() } + /// Values as their debug text, so that NaN, which doesn't equal itself, can be compared. fn shown(values: &[V]) -> Vec { values.iter().map(|v| format!("{v:?}")).collect() }