Refactor dispatch_sort() function