Skip to content

Stable variations of query/table sorting #1623

@iiYese

Description

@iiYese

Describe the problem you are trying to solve.
As discussed here the algorithm used to reorder tables is not stable. To get stable behavior you have to generate indices for flecs to use as a key, with those indices being the row index entites should have if a stable sorting algorithm was used for the actual key. Rust API example:

world.system_named::<&SequenceIndex>("Sequence sort")
    .kind::<flecs::pipeline::OnStore>()
    .order_by::<SequenceIndex>(SequenceIndex::sort_cmp)
    .each(|_| {});

//..

system!(world, &Anchor).write::<flecs::Wildcard>().run(|mut iter| {
    while iter.next() {
        if !iter.is_changed() {
            continue;
        }
        for (n, id) in iter
            .field::<Anchor>(0)
            .unwrap()
            .iter()
            .enumerate()
            .map(|(n, anch)| (iter.entity(n).id(), anch.offset))
            .collect::<Vec<_>>()
            .tap_mut(|ids| ids.sort_by_key(|(_, offset)| *offset))
            .into_iter()
            .map(|(id, _)| id)
            .enumerate()
        {
            id.entity_view(iter.world()).set(SequenceIndex(n));
        }
    }
});

Describe the solution you'd like
Make order_by stable or add order_by_stable variations.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions