indexOfObject:inSortedRange:options:usingComparator returns wrong index

361 views Asked by At

i have tried following method to search an object using binary search but the result is coming some garbage value.

    NSArray *orderedSet = [[NSArray alloc]initWithObjects:@2,@3,@5,nil];
    NSLog(@"%@",orderedSet);

    NSLog(@"%lu",(unsigned long)[orderedSet indexOfObject:@3 inSortedRange:NSMakeRange(0, 3) options:NSBinarySearchingFirstEqual usingComparator:^NSComparisonResult(id obj1, id obj2) {
        return [obj2 compare:obj1];
    }]);

It output the 9223372036854775807, Can anyone let me know what i am doing wrong here.

1

There are 1 answers

3
bLacK hoLE On

change short Range from (0,3) to (0,2)

   NSLog(@"%lu",(unsigned long)[orderedSet indexOfObject:@3 inSortedRange:NSMakeRange(0, 2) options:NSBinarySearchingFirstEqual usingComparator:^NSComparisonResult(id obj1, id obj2) {
        return [obj2 compare:obj1];
    }]);