{"record":{"id":"3c737dd6cb28f937","repo":"bevyengine/bevy","slug":"it-is-not-valid-to-call-sort-after-next","errorCode":null,"errorMessage":"it is not valid to call sort() after next()","messagePattern":"it is not valid to call sort\\(\\) after next\\(\\)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/bevy_ecs/src/query/iter.rs","lineNumber":1011,"sourceCode":"    /// # Panics\n    ///\n    /// This will panic if `next` has been called on `QueryIter` before, unless the underlying `Query` is empty.\n    fn sort_impl<L: ReadOnlyQueryData + SingleEntityQueryData + 'w>(\n        self,\n        f: impl FnOnce(&mut Vec<(L::Item<'_, '_>, NeutralOrd<Entity>)>),\n    ) -> QuerySortedIter<\n        'w,\n        's,\n        D,\n        F,\n        impl ExactSizeIterator<Item = Entity> + DoubleEndedIterator + FusedIterator + 'w,\n    > {\n        // On the first successful iteration of `QueryIterationCursor`, `archetype_entities` or `table_entities`\n        // will be set to a non-zero value. The correctness of this method relies on this.\n        // I.e. this sort method will execute if and only if `next` on `QueryIterationCursor` of a\n        // non-empty `QueryIter` has not yet been called. When empty, this sort method will not panic.\n        if !self.cursor.archetype_entities.is_empty() || !self.cursor.table_entities.is_empty() {\n            panic!(\"it is not valid to call sort() after next()\")\n        }\n\n        let world = self.world;\n\n        let query_lens_state = self.query_state.transmute_filtered::<(L, Entity), F>(world);\n\n        // SAFETY:\n        // `self.world` has permission to access the required components.\n        // The original query iter has not been iterated on, so no items are aliased from it.\n        // `QueryIter::new` ensures `world` is the same one used to initialize `query_state`.\n        let query_lens = unsafe { query_lens_state.query_unchecked_manual(world) }.into_iter();\n        let mut keyed_query: Vec<_> = query_lens\n            .map(|(key, entity)| (key, NeutralOrd(entity)))\n            .collect();\n        f(&mut keyed_query);\n        let entity_iter = keyed_query\n            .into_iter()\n            .map(|(.., entity)| entity.0)","sourceCodeStart":993,"sourceCodeEnd":1029,"githubUrl":"https://github.com/bevyengine/bevy/blob/396ca727080776bd313bb892423b7d94e03b81b4/crates/bevy_ecs/src/query/iter.rs#L993-L1029","documentation":"Panic from QueryIter::sort_impl: the iterator has already been partially consumed by next() (its iteration cursor set archetype/table entity slices), so the backing storage can no longer be safely sorted in place. Sorting is only valid on a fresh, unconsumed QueryIter over a non-empty query.","triggerScenarios":"Thrown at crates/bevy_ecs/src/query/iter.rs:1011 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Call sort()/sort_by_key() before any next() on the iterator","Use a fresh Query for sorting instead of reusing a partially consumed one","Collect into a Vec and sort that if iteration already began"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"396ca727080776bd313bb892423b7d94e03b81b4","analyzedAt":"2026-08-20T16:12:39.808Z","contentChangedAt":"2026-08-20T16:12:39.808Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}