Shouldn't you be using orddict here? There's no guarantee that TargetNodes is sorted definitely no guarantee that all TargetNodes sort before whatever is in ShardsByTargetNode0.