I think you should sort only when you're done looping over the array