NSMutableArray sorting - case insensitive

Kevin picture Kevin · Jun 20, 2012 · Viewed 8.3k times · Source

I am sorting an NSMutableArray as follows:

    NSSortDescriptor *sortDescriptor;
    sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:str_key ascending:bool_asc_desc] autorelease];
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
    NSArray *sortedArray;
    sortedArray = [ads_printers_array sortedArrayUsingDescriptors:sortDescriptors];

The problem is that this is case sensitive, and I would like to make it case insensitive. How can I do that? I tried reading the docs and found something like this:

sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:str_key ascending:bool_asc_desc selector: @selector(caseInsensitiveCompare)] autorelease];

However, I have no idea what I should be putting in the selector argument. Thanks!

Answer

karthika picture karthika · Apr 22, 2013
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:str_key 
    ascending:YES selector:@selector(caseInsensitiveCompare:)];

ads_printers_array = [ads_printers_array sortedArrayUsingDescriptors:[NSArray 
    arrayWithObject:sortDescriptor]];