Note that there are some explanatory texts on larger screens.

plurals
  1. PONSSortDescriptor with arbitrary sorting
    primarykey
    data
    text
    <p>I can't wrap my head around how to do arbitrary sorting with a NSSortDescriptor.</p> <p>I want to do something like this:</p> <pre><code>NSArray *sortAlgorithm = [NSArray arrayWithObjects:@"@", @"#", @"!", @"&amp;", @"r", @"a", nil]; NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES comparator: ^(id obj1, id obj2) { NSComparisonResult comparisonResult; //Some code that uses sortAlgorithm. return comparisonResult; } ]; </code></pre> <p>This would sort the objects by the key <code>name</code> so that any key that starts with <code>@</code>, e.g. <code>@home</code>, would come before any key that starts with <code>r</code>, e.g. <code>radical</code>, and that again would come before any key that starts with <code>a</code>, e.g. <code>anything</code>.</p> <p>The above is just an example. The point is to enable completely arbitrary sorting.</p> <p>This is to be used for a NSFetchedResultsController.</p> <p>What would the code for <em>//Some code that uses sortAlgorithm</em> look like?</p> <p><strong>EDIT:</strong></p> <p>Code surrounding my attempt to implement a sortDescriptor, as pr. <code>occulus'</code> suggestion:</p> <pre><code>- (NSFetchedResultsController *)fetchedResultsController { if (__fetchedResultsController) return __fetchedResultsController; NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; fetchRequest.entity = [NSEntityDescription entityForName:@"Tag" inManagedObjectContext:self.temporaryManagedObjectContext]; //NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO]; NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO comparator:^(id obj1, id obj2) { NSArray *sortAlgorithm = [NSArray arrayWithObjects:@"#", @"!", @"@", @".", nil]; NSString *obj1FirstChar = [(NSString *)obj1 substringToIndex:1]; NSString *obj2FirstChar = [(NSString *)obj2 substringToIndex:1]; int idx1 = [sortAlgorithm indexOfObject:obj1FirstChar]; int idx2 = [sortAlgorithm indexOfObject:obj2FirstChar]; if ( idx1 &lt; idx2 ) return NSOrderedAscending; else if ( idx1 &gt; idx2 ) return NSOrderedDescending; else return NSOrderedSame; }]; fetchRequest.sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; fetchRequest.fetchBatchSize = 20; NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.temporaryManagedObjectContext sectionNameKeyPath:nil cacheName:@"Tags"]; aFetchedResultsController.delegate = self; self.fetchedResultsController = aFetchedResultsController; [self.fetchedResultsController performFetch:nil]; return __fetchedResultsController; } </code></pre> <p>The commented-out sortDescriptor works.</p> <p>There is definitely a property called <code>name</code> on objects of entity "Tag". But even if there weren't, that doesn't seem to be the problem. Xcode doesn't seem to even be compiling that line of code (the sortDescriptor), which sounds ridiculous. Breakpoints are working just fine, but aren't breaking on that specific line of code.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload