Sort array without second loop