Correcting order of key-value pairs to match key sorting behavior