creating loop to sort