Diacritical sensitive sorting for objects derived from kernel data?

If I want to sort an array of elements alphabetically without NSDiacriticInsensitiveSearch option, I need to use NSSortDescriptor with comparator

  NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"SortTitle" ascending:YES comparator: ^(id obj1, id obj2) { NSInteger options = NSCaseInsensitiveSearch | NSNumericSearch // Numbers are compared using numeric value // | NSDiacriticInsensitiveSearch // Ignores diacritics (Γ’ == Γ‘ == a) | NSWidthInsensitiveSearch; // Unicode special width is ignored return [(NSString*)obj1 compare:obj2 options:options]; 

However, such an NSSortDescriptor with a comparator is not allowed to be used in NSFetchRequest . Therefore, should you sort the results after obtaining data from the master data? Is it computationally expensive in terms of performance?

thanks

+7
sorting ios objective-c
source share
1 answer

There are two approaches that I can think of:

  • Therefore should I sort the results after fetching them from core data? is one way to do this. Apple does not provide the exact complexity of sorting strings, but I think the big problem is having to retrieve all objects from persistent storage first. If you have a lot of data, this may interfere with your work. It’s best to profile it and only then decide if the performance is acceptable.

  • You can try using NSString methods that translate to SQL: localizedStandardCompare: localizedCompare: or localizedCaseInsensitiveCompare: A sort handle using any of these methods can be created as follows:

     sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"sortTitle" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)]; 

    If none of these methods sorts the data the way you want, you can also pre-process it in advance, for example. when changing the name, you delete the diacritics, etc. (see Normalize User-Generated Content in NSHipster - CFStringTransform ). UPDATE:. Suppose the title attribute is named title , and the sorting title is called sortTitle . In a subclass of NSManagedObject you can override didChangeValueForKey: as follows:

     - (void)didChangeValueForKey:(NSString *)key { [super didChangeValueForKey:key]; if ([key isEqualToString:@"title"]) { NSString *cleanTitle = [self.title mutableCopy]; CFStringTransform((__bridge CFMutableStringRef)(cleanTitle), NULL, kCFStringTransformStripCombiningMarks, NO); self.sortTitle = [cleanTitle copy]; } } 
+13
source share

All Articles