Sort array to avoid restarts