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 8129521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:20:31+00:00 2026-06-06T08:20:31+00:00

It’s my first post so sorry if I may not respect all the conventions

  • 0

It’s my first post so sorry if I may not respect all the conventions even though I’ll try my best. I have always found solutions to my problems on SO before but I’m completely stuck on a rather complex Cocoa problem.

I am trying to achieve a complex sort on a list of CoreData objects. I have a catalog composed of Book objects, which can be part of Saga (first book and its sequels). The simplified structures look like this:

@interface Book : NSManagedObject
@property (nonatomic, retain) NSString * title;
@property (nonatomic, retain) NSNumber * tomaison; //volume numbering
@property (nonatomic, retain) Saga *fromSaga;

@interface Saga : NSManagedObject
@property (nonatomic, retain) NSString * title;

I’m trying to perform a query on my CoreData db, on Book:

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Book" inManagedObjectContext:context];

and I need a sort in three steps:

1) Sort by book’s Genre (not included in the code above because it’s not needed), which is performed with:

NSSortDescriptor* mainSort = [[NSSortDescriptor alloc] initWithKey:@"ofGenre.title" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];

2) Sort by Saga title if the book is part of a Saga

NSSortDescriptor* secondarySort = [[SagaTitleSortDescriptor alloc] initWithKey:@"fromSaga" ascending:YES];

Where the custom sort descriptor is defined by:

#define NULL_OBJECT(a) ((a) == nil || [(a) isEqual:[NSNull null]])
@interface SagaTitleSortDescriptor : NSSortDescriptor {}
@end
@implementation SagaTitleSortDescriptor
- (id)copyWithZone:(NSZone*)zone
{
    return [[[self class] alloc] initWithKey:[self key] ascending:[self ascending] selector:[self selector]];
}
- (NSComparisonResult)compareObject:(id)object1 toObject:(id)object2
{
    if (NULL_OBJECT([object1 valueForKeyPath:[self key]])) {
        if (NULL_OBJECT([object2 valueForKeyPath:[self key]])) 
            return NSOrderedSame;
        return NSOrderedDescending;        
    }
    if (NULL_OBJECT([object2 valueForKeyPath:[self key]])) {
        return NSOrderedAscending;
    }
    return [super compareObject:[(Saga*)object1 title] toObject:[(Saga*)object2 title]];
}
@end

3) Sort by Volume Numbering IF it is part of a Saga, otherwise, sort by Book Title. Here is my problem, as I don’t know what key to send and what to put in my descriptor (I’m not even sure it’s possible).

NSSortDescriptor* thirdSort = [[SagaTomaisonOrBookTitleSortDescriptor alloc] initWithKey:@"self" ascending:YES];  

So far, I’ve found that @”self” allows to send the object being queried but it doesn’t seem to allow queries of parameters inside the sent object. For reference, there is some code I tried:

- (NSComparisonResult)compareObject:(id)object1 toObject:(id)object2
{
    if (NULL_OBJECT([(Book*)object1 fromSaga]) && NULL_OBJECT([(Book*)object2 fromSaga])) {
        return [super compareObject:[(Book*)object1 title] toObject:[(Book*)object2 title]];
    }
    if (NULL_OBJECT([(Book*)object1 fromSaga])) {
        return NSOrderedDescending;
    }
    if (NULL_OBJECT([(Book*)object2 fromSaga])) {
        return NSOrderedAscending;
    }
    return [super compareObject:[(Book*)object1 tomaison] toObject:[(Book*)object1 tomaison]];
}

Any idea what I can and should do ?

Thanks !

EDIT: there was a type in the last line

  • 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-06-06T08:20:33+00:00Added an answer on June 6, 2026 at 8:20 am

    If you’re not using an SQL-based store, you could do this by passing self as your key and compare: as the selector, then implement that custom selector. That is, make a method on your book class called compare: and just have it do all of your logic rather than using multiple sort descriptors.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a text area in my form which accepts all possible characters from
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.