Sort keys before comparing each key