sorting before counting