Ok, the bug makes sense. Thanks for noticing it, and explaining it clearly!

I'm not sure why you say "the entire functionality change depends on it", or why it would be more of an issue with this pull request than it was previously. ISTM that the bug exists prior to this pull request and is equally possible to trigger before or after this pull request.

Given that, I've gone ahead and fixed it, with its own dedicated test, in a separate commit: 4b6a8000503ce2cf754ec2b5974f9b4e5e0c0ebf

You'll notice I put the sort in the `iterator()` method, closer to the problem it is solving.