Reorder uniq/collect/sort for better efficiency