Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Added example of entity sorting by components #1817

Closed
wants to merge 9 commits into from
Prev Previous commit
Next Next commit
Fixes and review issues
  • Loading branch information
YohDeadfall committed Apr 17, 2021
commit 7d8ef4e6a51cce52b1cd9cf5d03f48a4bc49aebd
17 changes: 9 additions & 8 deletions crates/bevy_ecs/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,19 +451,20 @@ impl World {
/// ```
/// use bevy_ecs::{entity::Entity, world::World};
/// let mut world = World::new();
/// let a = world.spawn().insert_bundle((2, "abc")).id();
/// let b = world.spawn().insert_bundle((3, "xyz")).id();
/// let c = world.spawn().insert_bundle((1, "def")).id();
/// let mut entities = world.query::<(Entity, &i32, &str)>()
/// .map(|(e, &i &s)| (e, i, &s)) // Copy out of the world
/// let a = world.spawn().insert_bundle((2, 4.0)).id();
/// let b = world.spawn().insert_bundle((3, 5.0)).id();
/// let c = world.spawn().insert_bundle((1, 6.0)).id();
/// let mut entities = world.query::<(Entity, &i32, &f32)>()
/// .iter(&world)
/// .collect::<Vec<_>>();
/// // Sort by `i32` component
/// entities.sort_by(|x, y| x.1.cmp(&y.1));
YohDeadfall marked this conversation as resolved.
Show resolved Hide resolved
/// for (index, entity) in entities.iter().enumerate() {
/// match index {
/// 0 => assert!(entity == (c, 1, "def")),
/// 1 => assert!(entity == (a, 2, "abc")),
/// 2 => assert!(entity == (b, 3, "xyz")),
/// 0 => assert_eq!(entity, &(c, &1, &6.0)),
/// 1 => assert_eq!(entity, &(a, &2, &4.0)),
/// 2 => assert_eq!(entity, &(b, &3, &5.0)),
/// _ => panic!("not expected"),
/// }
/// }
/// ```
Expand Down