Skip to content


Add case insensitive sort option #135

dbowen opened this Issue · 13 comments

9 participants


It’d be nice have a case insensitive version of all the fetching functions with a sort option. Currently we have to do this with a predicate from what I’ve seen, but it’d be nice to have this option as I don’t believe its too uncommon.


I think I miscommunicated, it would be for the ordering of the results, not on the property names themselves. It's more of a suggestion for simplicity rather than necessary, as we can do it with predicates. However, I think we may end up writing it in for ourselves anyway as an option just for simplicity. Is this something you would want a pull request for?

As an example, lets assume we have an inventory system. The user enters various details, mainly product name. Regardless of wether or not the user entered the data as "Pencil" or "pencil", all things starting with the letter 'p' should be in the same area of the tableview based on the product name.


I ran into the issue today too. I have a lists of contacts that are pulled from LinkedIn. Out of about 130 contacts, 2 people have written their first and last name in lowercase.

I use:
self.fetchedResultsController = [Contact fetchAllSortedBy:@"firstName" ascending:YES withPredicate:nil groupBy:@"firstName" delegate:self];

And this gives me a list of my contacts sorted by first name, but divides the list up into two, the longer one which has contacts that use uppercase letter for their first name and then at the bottom, the two contacts that have used lower case for their names at the bottom (sorted alphabetically by first name).


I asked Apple about this at WWDC, and their recommended solution is to create a normalized attribute to sort on. Not exactly ideal, but it works for now.

In your case I would add the attribute normalizedFirstName, then override the setter for firstName to also set normalizedFirstName as [firstName lowercaseString].


Thanks for the follow-up. Strange case, I can't imagine having to sort like this at all, but maybe I am biased due to my use-case.


Hi. I ran into the same issue. I have a simple lookup table of names that I'd like to order without considering the case. I could follow Apple suggestion and duplicate the name field to store the normalized version, but it seems a waste of space and add complexity to the model. I'm not so happy with it.

How about a solution like this one: RestKit/RestKit#702


PS: thanks, MR is awesome

Magical Panda Software member

I ran into the same issue. MR_finadAllSortedByCaseinsensitive would be great.


This might be against software engineering concepts and mess up performance

But to get things done.

I am directly accessing
(NSFetchedResultsController *) MR_fetchController:(NSFetchRequest *)request delegate:(id<NSFetchedResultsControllerDelegate>)delegate useFileCache:(BOOL)useFileCache groupedBy:(NSString *)groupKeyPath inContext:(NSManagedObjectContext *)context;

from category method in NSManagedObject (MagicalFinders_CustomRequest)


NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%@ IN planets.galaxy.ID", self.galaxy.ID];
NSFetchRequest *request = [Planets MR_requestAllSortedBy:nil
                                                inContext:[NSManagedObjectContext MR_contextForCurrentThread]];
    NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
    request.sortDescriptors = [NSArray arrayWithObject:sort];
self.planetsFetchController = [Planets MR_fetchAllWithRequest:request

cc @ethanparker


@mohith that's just fine, and exactly what I would have done.

Assuming it works. Keep in mind that the block-based sort descriptors won't work against a SQLite store — I'm unsure of whether the same is true of the selector-based sort descriptors such as the one you've provided here.

Does it work, and are you using a SQLite based persistent store?


@tonyarnold Yep it worked.I am using SQLite based persistent store (NSSQLiteStoreType)

@naqi naqi referenced this issue in naqi/MagicalRecord

Allow for case insensitive search #1


Merge from @naqi could be the reason of crash:

-[__NSDate localizedCaseInsensitiveCompare:]: unrecognized selector sent to instance 0x17000bc50

I think better to introduce new MR_fetchAll... with NSSortDescriptor as parameter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.