completed doSort function