From d64aee2d85a03ce6cecf20ff6ab53b43056e4860 Mon Sep 17 00:00:00 2001 From: Romulus21 Date: Fri, 9 Oct 2026 22:28:03 +0200 Subject: [PATCH] =?UTF-8?q?Trie=20les=20articles=20par=20cat=C3=A9gorie=20?= =?UTF-8?q?puis=20par=20ordre=20alphab=C3=A9tique?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Les catégories suivent l'ordre de l'enum (parcours du magasin) ; les noms sont comparés sans tenir compte de la casse ni des accents. Co-Authored-By: Claude Opus 5.5 --- src/app/mod.rs | 4 +++- src/model.rs | 56 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/app/mod.rs b/src/app/mod.rs index 3695d6d..df1a359 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -261,7 +261,9 @@ fn ListsContent( .into_any(); }; - // Items with pending checkbox changes applied. + // Items with pending checkbox changes applied, by category then name. + let mut items = items; + items.sort_by_cached_key(Item::sort_key); let items = StoredValue::new(items); let current = Memo::new(move |_| { let optimistic = ctx.optimistic.get(); diff --git a/src/model.rs b/src/model.rs index bcf72fe..4618351 100644 --- a/src/model.rs +++ b/src/model.rs @@ -98,6 +98,34 @@ pub struct Item { pub checked: bool, } +impl Item { + /// Display order: by category (aisle order of `Category`), then by name. + pub fn sort_key(&self) -> (Category, String) { + (self.category, fold_name(&self.name)) + } +} + +/// Lowercases and strips French accents so "Œufs" sorts with "o" and +/// "Épinards" with "e". +fn fold_name(name: &str) -> String { + let mut folded = String::with_capacity(name.len()); + for c in name.chars().flat_map(char::to_lowercase) { + match c { + 'à' | 'â' | 'ä' => folded.push('a'), + 'é' | 'è' | 'ê' | 'ë' => folded.push('e'), + 'î' | 'ï' => folded.push('i'), + 'ô' | 'ö' => folded.push('o'), + 'ù' | 'û' | 'ü' => folded.push('u'), + 'ÿ' => folded.push('y'), + 'ç' => folded.push('c'), + 'œ' => folded.push_str("oe"), + 'æ' => folded.push_str("ae"), + _ => folded.push(c), + } + } + folded +} + #[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)] pub struct Quantity { pub amount: f64, @@ -148,7 +176,8 @@ impl Unit { } } -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +/// Variant order is the display order of items. +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] pub enum Category { FruitsLegumes, Frais, @@ -270,6 +299,31 @@ mod tests { assert_eq!(user("élise").initials(), "ÉL"); } + #[test] + fn items_sort_by_category_then_name() { + let item = |name: &str, category| Item { + id: 0, + list_id: 0, + name: name.into(), + quantity: None, + icon: Icon::ALL[0], + category, + checked: false, + }; + let mut items = vec![ + item("Pain", Category::Boulangerie), + item("Yaourts", Category::Frais), + item("Œufs", Category::Frais), + item("beurre", Category::Frais), + item("Épinards", Category::FruitsLegumes), + item("Citrons", Category::FruitsLegumes), + item("Olives", Category::Frais), + ]; + items.sort_by_key(Item::sort_key); + let names: Vec<&str> = items.iter().map(|i| i.name.as_str()).collect(); + assert_eq!(names, ["Citrons", "Épinards", "beurre", "Œufs", "Olives", "Yaourts", "Pain"]); + } + #[test] fn text_roundtrip() { for icon in Icon::ALL {