refactored sortedArray()