refactor sort function in collection