sort() only once in state.Compiler.order_chunks()

refactor a bit further per @gldnspud in #4836