Get index of object in array to look up corresponding object in other array Get index of object in array to look up corresponding object in other array arrays arrays

Get index of object in array to look up corresponding object in other array


NSArray has a method called indexOfObject that will return either the lowest index whose corresponding array value is equal to anObject or NSNotFound if no such object is found. If your array of names is unsorted, then use this to get the index that you can then plug in to the Yes/No array. That is, something along these lines:

NSString *answer = nil;NSUInteger index = [namesArray indexOfObject:@"John Smith"];if (index != NSNotFound) {    answer = [yesNoArray objectAtIndex:index];}return answer;

Because Bavarious asks questions where I assume, here's a better way when the array of names is sorted alphabetically.

int index = [self findName:@"John Smith"];NSString *answer = nil;if (index >= 0) {    answer = [yesNoArray objectAtIndex:index];}return answer;

where the function findName is a simple binary search:

-(int)findName:(NSString *)name {    int min, mid, max;    NSComparisonResult comparisonResult;    min = 0;    max = [namesArray count]-1;    while (min <= max) {        mid = min + (max-min)/2;        comparisonResult = [name compare:[namesArray objectAtIndex:mid]];        if (comparisonResult == NSOrderedSame) {            return mid;        } else if (comparisonResult == NSOrderedDescending) {            min = mid+1;        } else {            max = mid-1;        }    }       return -1;  }


Trying to keep two arrays synchronized is just asking for trouble. It can be done, of course, but whenever you modify one array, you have to remember to make a corresponding change to the other. Do yourself a favor and avoid that entire class of bugs by rethinking the way you're storing data.

In this case, you've got a {person, boolean} pair. One option is to store each pair as a dictionary, and then keep an array of those dictionaries. This would be a particularly good plan if you might expand the number of pieces of data beyond the two that you have. Another option would be to just use a dictionary where keys are person names and the values are your yes/no values. This makes the answer to your question very simple:

NSString *yesOrNo = [personDictionary objectForKey:personName];

Getting back to your original question, where you still have the two arrays, the easiest thing to do is to iterate over the person array until you find the person you're looking for, get the index of that name, and then look up the corresponding value in the yes/no array:

for (person in peopleArray) {    if ([person isEqualToString:thePersonYoureLookingFor]) {        yesNoValue = [yesNoArray objectAtIndex:[peopleArray indexOfObject:person];        break;    }}

That's fine if the number of people in the list isn't too large. If the list could be large, then you'll want to keep the person array sorted so that you can do a binary search. The trouble there, though, is that you're yes/no array is separate, so sorting the personArray while keeping the yes/no array in the right order becomes complicated.


You can also use below of the code, May its useful to you,

NSSortDescriptor *_lastDescriptor = [[NSSortDescriptor alloc] initWithKey:@"" ascending:YES];NSArray *_lastArray = [NSArray arrayWithObject:_lastDescriptor];firstCharacterArray = (NSMutableArray *)[[nameIndexesDictionary allKeys]      sortedArrayUsingDescriptors:_lastArray];//firstCharacterArray = (NSMutableArray *)[[nameIndexesDictionary allKeys]                    sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];for (NSString *eachlastIndex in firstCharacterArray){NSSortDescriptor *lastDescriptor = [[NSSortDescriptor alloc] initWithKey:@""                                                               ascending:YES];//selector:@selector(localizedCaseInsensitiveCompare:)] ;NSArray *descriptorslast = [NSArray arrayWithObject:lastDescriptor];[[nameIndexesDictionary objectForKey:eachlastIndex]     sortUsingDescriptors:descriptorslast];[lastDescriptor release];}