refactor sort