Skip to content

Commit ea45962

Browse files
committed
Added example of entity sorting by components
1 parent c2a427f commit ea45962

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

crates/bevy_ecs/src/core/world.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,14 @@ impl World {
236236
/// let a = world.spawn((123, true, "abc"));
237237
/// let b = world.spawn((456, false));
238238
/// let c = world.spawn((42, "def"));
239-
/// let entities = world.query::<(Entity, &i32, &bool)>()
239+
/// let mut entities = world.query::<(Entity, &i32, &bool)>()
240240
/// .map(|(e, &i, &b)| (e, i, b)) // Copy out of the world
241241
/// .collect::<Vec<_>>();
242+
/// // Sort by `i32` component in reverse and by `bool` then
243+
/// entities.sort_by(|x, y| x.1.cmp(&y.1).reverse().then(x.2.cmp(&y.2)))
242244
/// assert_eq!(entities.len(), 2);
243-
/// assert!(entities.contains(&(a, 123, true)));
244-
/// assert!(entities.contains(&(b, 456, false)));
245+
/// assert!(entities[0] == (b, 456, false));
246+
/// assert!(entities[1] == (a, 123, true));
245247
/// ```
246248
#[inline]
247249
pub fn query<Q: WorldQuery>(&self) -> QueryIter<'_, Q, ()>

0 commit comments

Comments
 (0)