Add sort_by_count function