Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6857841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:04:52+00:00 2026-05-27T02:04:52+00:00

I can’t wrap my head around how to do arbitrary sorting with a NSSortDescriptor.

  • 0

I can’t wrap my head around how to do arbitrary sorting with a NSSortDescriptor.

I want to do something like this:

NSArray *sortAlgorithm = [NSArray arrayWithObjects:@"@", @"#", @"!", @"&", @"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;
    }
];

This would sort the objects by the key name so that any key that starts with @, e.g. @home, would come before any key that starts with r, e.g. radical, and that again would come before any key that starts with a, e.g. anything.

The above is just an example. The point is to enable completely arbitrary sorting.

This is to be used for a NSFetchedResultsController.

What would the code for //Some code that uses sortAlgorithm look like?

EDIT:

Code surrounding my attempt to implement a sortDescriptor, as pr. occulus' suggestion:

- (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 < idx2 )
            return NSOrderedAscending;
        else if ( idx1 > 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;
}

The commented-out sortDescriptor works.

There is definitely a property called name 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:04:53+00:00Added an answer on May 27, 2026 at 2:04 am

    You need to pull out the first characters of obj1 and obj2 strings as NSStrings, find their indexes in your arbitrary ordering array, then compare the positions.

    Something like this: (put this code in your ^ block)

    NSString *obj1FirstChar = [(NSString *)obj1 substringToIndex:1];
    NSString *obj2FirstChar = [(NSString *)obj2 substringToIndex:1];
    int idx1 = [sortAlgorithm indexOfObject:obj1FirstChar];
    int idx2 = [sortAlgorithm indexOfObject:obj2FirstChar];
    
    // NOTE: we haven't dealt with the case where one of the
    // chars wasn't found in the array of characters. A strategy
    // for this would need to be decided.
    
    if (idx1 == idx2) {
        return NSOrderedSame;
    }
    if (idx1 < idx2) {
        return NSOrderedAscending;
    }
    return NSOrderedDescending;
    

    Not tested, may require a little tweaking. Watch out for upper/lower case character differences.

    UPDATE: It seems there are problems with using custom sort descriptors and SQL backed core data stores. Please see this question for more info.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can I run this in a Windows command prompt like I can run it
can someone show me the regex for this preg_match. I want to make sure
Can someone help me? I am trying to do something like the following: #include
Can anyone tell me how I can display a status message like 12 seconds
Can anybody help me? What should be the datatype for this type -07:00:00 of
Can someone guide me on a possible solution? I don't want to use /bin/cp
can someone explain why the compiler accepts only this code template<typename L, size_t offset,
Can't figure out how to do this in a pretty way : I have
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
can anyone tell me why this doesn't work? db = openOrCreateDatabase(database.db, SQLiteDatabase.CREATE_IF_NECESSARY, null); db.setLocale(Locale.getDefault());

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.