I don't know if you want anything we have in the Java code, but this is a tickler to have a look. I just found some insidious issues relative to repeated counts that you may care about. If you have a stable sort, these may not have impacted you.