fixing bugs for sortedIndex