sortArray function is working correctly