-2

Can someone exlain this code in detail and suggest how I can sort on the name?

- (void)handleSearchForTerm:(NSString *)searchTerm {

selectButton.enabled = NO;

NSMutableArray *sectionsToRemove = [[NSMutableArray alloc] init];
[self resetSearch];

for (NSString *key in self.keys) {
    NSMutableArray *array = [Categories valueForKey:key];
    NSMutableArray *toRemove = [[NSMutableArray alloc] init];
    for (NSString *name in array) {
        if ([name rangeOfString:searchTerm 
                        options:NSCaseInsensitiveSearch].location == NSNotFound)
            [toRemove addObject:name];
    }

    if ([array count] == [toRemove count])
        [sectionsToRemove addObject:key];

    [array removeObjectsInArray:toRemove];
    [toRemove release];
}
[self.keys removeObjectsInArray:sectionsToRemove];
[sectionsToRemove release];
[table reloadData];
}
Jules
  • 7,568
  • 14
  • 102
  • 186
  • Do some search on SO ..Use following link to sort NSMutableArray http://stackoverflow.com/questions/805547/how-to-sort-an-nsmutablearray-with-custom-objects-in-it http://stackoverflow.com/questions/2766994/objective-c-sorting-nsmutablearray-containing-nsmutablearrays http://stackoverflow.com/questions/1844031/how-to-sort-nsmutablearray-using-sortedarrayusingdescriptors – Jhaliya - Praveen Sharma May 30 '11 at 11:21
  • 1
    why are you using code which you can't understand? – Eimantas May 30 '11 at 11:24
  • 1
    Try codereview.stackexchange.com. Also - this method doesn't return anything, it performs a delete. Is this just code you've found somewhere, or are you trying to do something special that you aren't explaining? – Abizern May 30 '11 at 11:39

1 Answers1

4
- (void)handleSearchForTerm:(NSString *)searchTerm {
  selectButton.enabled = NO;
  NSMutableArray *sectionsToRemove = [[NSMutableArray alloc] init]; //creating an mutable array, which can be altered in progress.
  [self resetSearch]; //calling some other method not displayed in the code here
  for (NSString *key in self.keys) { //for each key, 
     NSMutableArray *array = [Categories valueForKey:key];  //you get the key's category
     NSMutableArray *toRemove = [[NSMutableArray alloc] init]; //and initialize the array for items you wish to remove
     for (NSString *name in array) { //then, for each name
         if ([name rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location == NSNotFound)
             [toRemove addObject:name];
             //you check if the name is in range of the searchterm, with which you call this function
             //if you don't find it, you add it to the removal list
     }
     if ([array count] == [toRemove count])
         [sectionsToRemove addObject:key]; //if you haven't found any name, it means you've added all the names in the toRemove array
     [array removeObjectsInArray:toRemove]; //that means the count of both arrays are the same    
     [toRemove release]; //so you remove that section entirely, since there is no result there
 }
 [self.keys removeObjectsInArray:sectionsToRemove]; //you remove all the keys which aren't found
 [sectionsToRemove release]; //leaving you the keys which are found
 [table reloadData]; //you reload the table with the found results only
}

I hope it all made sense, I did my best commenting it ;)

Good luck.

Joetjah
  • 6,292
  • 8
  • 55
  • 90
  • +1 for effort - maybe the OP will learn something about why this code doesn't do what he thinks it does. – Abizern May 30 '11 at 12:02
  • thanks. This method is used with a search bar, to limit results shown in a tableview by whats typed. Your commenatry helped as I wasn't 100% sure about the code. However, it does confirm that its not this method which is determing the sort of the array contents. – Jules May 30 '11 at 13:17
  • @Jules: exactly right. Your code removes all items that don't fit with your search keyword, leaving the 'found results' alone. Thats all the code does ;) Good luck :) – Joetjah May 30 '11 at 13:20