Skip to content

Commit 5288be7

Browse files
authored
Expose sorting methods in Children (#8522)
# Objective - For many UI use cases (e.g. tree views, lists), it is important to be able to imperatively sort child nodes. - This also enables us to eventually support something like the [`order`](https://developer.mozilla.org/en-US/docs/Web/CSS/order) CSS property, that declaratively re-orders flex box items by a numeric value, similar to z-index, but in space. ## Solution We removed the ability to directly construct `Children` from `&[Entity]` some time ago (#4197 #5532) to enforce consistent hierarchies ([RFC 53](https://github.com/bevyengine/rfcs/blob/main/rfcs/53-consistent-hierarchy.md)). If I understand it correctly, it's currently possible to re-order children by using `Children::swap()` or `commands.entity(id).replace_children(...)`, however these are either too cumbersome, needlessly inefficient, and/or don't take effect immediately. This PR exposes the in-place sorting methods from the `slice` primitive in `Children`, enabling imperatively sorting children in place via `&mut Children`, while still preserving consistent hierarchies. --- ## Changelog ### Added - The sorting methods from the `slice` primitive are now exposed by the `Children` component, allowing imperatively sorting children in place (Useful for UI scenarios such as lists)
1 parent eebc92a commit 5288be7

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

crates/bevy_hierarchy/src/components/children.rs

+77
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,83 @@ impl Children {
4848
pub fn swap(&mut self, a_index: usize, b_index: usize) {
4949
self.0.swap(a_index, b_index);
5050
}
51+
52+
/// Sorts children [stably](https://en.wikipedia.org/wiki/Sorting_algorithm#Stability)
53+
/// in place using the provided comparator function.
54+
///
55+
/// For the underlying implementation, see [`slice::sort_by`].
56+
///
57+
/// For the unstable version, see [`sort_unstable_by`](Children::sort_unstable_by).
58+
///
59+
/// See also [`sort_by_key`](Children::sort_by_key), [`sort_by_cached_key`](Children::sort_by_cached_key).
60+
pub fn sort_by<F>(&mut self, compare: F)
61+
where
62+
F: FnMut(&Entity, &Entity) -> std::cmp::Ordering,
63+
{
64+
self.0.sort_by(compare);
65+
}
66+
67+
/// Sorts children [stably](https://en.wikipedia.org/wiki/Sorting_algorithm#Stability)
68+
/// in place using the provided key extraction function.
69+
///
70+
/// For the underlying implementation, see [`slice::sort_by_key`].
71+
///
72+
/// For the unstable version, see [`sort_unstable_by_key`](Children::sort_unstable_by_key).
73+
///
74+
/// See also [`sort_by`](Children::sort_by), [`sort_by_cached_key`](Children::sort_by_cached_key).
75+
pub fn sort_by_key<K, F>(&mut self, compare: F)
76+
where
77+
F: FnMut(&Entity) -> K,
78+
K: Ord,
79+
{
80+
self.0.sort_by_key(compare);
81+
}
82+
83+
/// Sorts children [stably](https://en.wikipedia.org/wiki/Sorting_algorithm#Stability)
84+
/// in place using the provided key extraction function. Only evaluates each key at most
85+
/// once per sort, caching the intermediate results in memory.
86+
///
87+
/// For the underlying implementation, see [`slice::sort_by_cached_key`].
88+
///
89+
/// See also [`sort_by`](Children::sort_by), [`sort_by_key`](Children::sort_by_key).
90+
pub fn sort_by_cached_key<K, F>(&mut self, compare: F)
91+
where
92+
F: FnMut(&Entity) -> K,
93+
K: Ord,
94+
{
95+
self.0.sort_by_cached_key(compare);
96+
}
97+
98+
/// Sorts children [unstably](https://en.wikipedia.org/wiki/Sorting_algorithm#Stability)
99+
/// in place using the provided comparator function.
100+
///
101+
/// For the underlying implementation, see [`slice::sort_unstable_by`].
102+
///
103+
/// For the stable version, see [`sort_by`](Children::sort_by).
104+
///
105+
/// See also [`sort_unstable_by_key`](Children::sort_unstable_by_key).
106+
pub fn sort_unstable_by<F>(&mut self, compare: F)
107+
where
108+
F: FnMut(&Entity, &Entity) -> std::cmp::Ordering,
109+
{
110+
self.0.sort_unstable_by(compare);
111+
}
112+
113+
/// Sorts children [unstably](https://en.wikipedia.org/wiki/Sorting_algorithm#Stability)
114+
/// in place using the provided key extraction function.
115+
///
116+
/// For the underlying implementation, see [`slice::sort_unstable_by_key`].
117+
///
118+
/// For the stable version, see [`sort_by_key`](Children::sort_by_key).
119+
///
120+
/// See also [`sort_unstable_by`](Children::sort_unstable_by).
121+
pub fn sort_unstable_by_key<K, F>(&mut self, compare: F)
122+
where
123+
F: FnMut(&Entity) -> K,
124+
K: Ord,
125+
{
126+
self.0.sort_unstable_by_key(compare);
127+
}
51128
}
52129

53130
impl Deref for Children {

0 commit comments

Comments
 (0)