refactor sort utils and tests