#Sorting an Iterator based on a value inside a queried struct

6 messages · Page 1 of 1 (latest)

modest lance
#

Say i have the following struct and resource:

#[derive(Component, Debug, Clone)]
pub struct TestStruct{
    pub index: f32,
    pub test_value: f32,
//other stuff
}

#[derive(Resource, Debug, Clone)]
pub struct StructContainer{
    structs: Vec<TestStruct>,
}

and an update function:

fn set_shape_container(
    mut structs_q: Query<&mut AABB>,
    mut struct_holder: ResMut<StructContainer>,){

for (struct) in structs_q.iter_mut(){

  struct_holder.structs.push(struct);

  
}

what i was hoping for is to iterate over the structs query in a way where the structs in the array will be sorted so that structs will be before other structs that have a higher index value (it can be changed to a u32 if required) then themselves, is this possible with queries?

last oriole
#

Is your TestStruct actually being used as a component on entities? Or is it only used by the StructContainer?

modest lance
#

as a component on entities, the structs vec will be passed into a shader

last oriole
#

You can sort your Vec by TestStruct::index for starters. Depends on how often it's updated whether that's good enough.

If you're inserting into it often, then maybe you want to use a BTreeMap<f32 or u32, TestStruct>. That'd be the typically sorted collection for rust. It's sorted by the key.

But I don't believe the query system is going to do any sorting for you.

mossy quartz
#

And QueryIter should be getting some handy sort methods in 0.14 (though I don’t believe the sort is cached or anything)