Refactor sort-by-second to DRY it