Improve sort_by_key() performance