Refactor sort to search