Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion crates/data/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down Expand Up @@ -785,6 +788,18 @@ mod tests {
assert_eq!(std::mem::size_of::<Value>(), 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);
Expand Down
80 changes: 79 additions & 1 deletion crates/ui-egui/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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`, which isn't a total order: see `cmp_values`.
let o = cmp_values(ds.value(*a, c), ds.value(*b, c));
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
if asc { o } else { o.reverse() }
});
}
Expand Down Expand Up @@ -1168,6 +1170,82 @@ 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<String> {
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();
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<String> {
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;
}
}
let reversed: Vec<_> = expected.iter().rev().cloned().collect();
assert_eq!(row_order(&values, false, "", None), reversed);
}

#[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).
Expand Down
Loading