perf(table): sort optimizations

This commit is contained in:
Johann Tuffe 2025-04-12 00:19:43 +08:00 • committed by GitHub
parent 924a9ff371
commit 67df54f383
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -347,19 +347,20 @@ where
/// Sorts items in the model, this should be called before it is drawn after all items have been added for the view /// Sorts items in the model, this should be called before it is drawn after all items have been added for the view
pub fn sort(&mut self, category: Category, ascending: bool) { pub fn sort(&mut self, category: Category, ascending: bool) {
self.sort = Some((category, ascending)); match self.sort {
let mut order: Vec<Entity> = self.order.iter().cloned().collect(); Some((cat, asc)) if cat == category && asc == ascending => return,
order.sort_by(|entity_a, entity_b| { Some((cat, _)) if cat == category => self.order.make_contiguous().reverse(),
if ascending { _ => {
self.item(*entity_a) self.order.make_contiguous().sort_by(|entity_a, entity_b| {
.unwrap() let cmp = self
.compare(self.item(*entity_b).unwrap(), category) .items
} else { .get(*entity_a)
self.item(*entity_b) .unwrap()
.unwrap() .compare(self.items.get(*entity_b).unwrap(), category);
.compare(self.item(*entity_a).unwrap(), category) if ascending { cmp } else { cmp.reverse() }
});
} }
}); }
self.order = order.into(); self.sort = Some((category, ascending));
} }
} }