Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add case insensitive sort option #135

Open
dbowen opened this Issue · 13 comments

9 participants

@dbowen

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.

@magicalpanda
@dbowen

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.

@magicalpanda
@runmad

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).

@dbowen

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].

@runmad

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.

@mbigatti

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

Regards,
MB

PS: thanks, MR is awesome

@casademora
Owner
@ethanparker

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

@mohith

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)

and

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%@ IN planets.galaxy.ID", self.galaxy.ID];
NSFetchRequest *request = [Planets MR_requestAllSortedBy:nil
                                                ascending:NO
                                            withPredicate:predicate
                                                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
                            groupBy:nil
                           delegate:self];

cc @ethanparker

@tonyarnold
Owner

@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?

@mohith

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

@naqi naqi referenced this issue in naqi/MagicalRecord
Merged

Allow for case insensitive search #1

@andreyslyusar

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.