fix code for computing least sort of KItems

I fully expect removing this nasty hack from Context.java will break things. So don't review until you see the tests pass.
Is this change related to this fix?