Fixed error caused due to use of sort function inside array indexing