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

  • SEARCH
  • Home
  • 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 8155969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:49:02+00:00 2026-06-06T16:49:02+00:00

I have the following sort descriptors that sort an array of my business objects,

  • 0

I have the following sort descriptors that sort an array of my business objects, ready to be displayed in a table, I’m starting off with some sample sorting code from a previous SO question

NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"awardedOn" ascending:NO];
NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor1, sortDescriptor2, nil];
NSArray *sortedArray = [returnable sortedArrayUsingDescriptors:sortDescriptors];

The objects that I’m displaying all will have a title. Only some of them will have an “awardedOn” set, which is an NSDate.

What I want to do:

  1. Sort entire array so all the objects with an “awardedOn” set are
    displayed at the top
  2. Within the two “sets”, order them alphabetically
  3. I don’t care about the actual value of the date, I’m more interested
    if it exists or not

Something like this (Titles, the bold ones have a value for awardedOn)

  • Awesome
  • Better
  • Cool
  • Another
  • Another One
  • One more
  • Yet Another
  • 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-06T16:49:04+00:00Added an answer on June 6, 2026 at 4:49 pm

    You should be able to do that using two descriptors like you first said, first by awardedOn, then by title. However, you need to provide a custom NSSortDescriptor for the awardedOn sort that looks someting like this:

    #define NULL_OBJECT(a) ((a) == nil || [(a) isEqual:[NSNull null]]) 
    @interface AwardedOnSortDescriptor : NSSortDescriptor {} 
    @end 
    @implementation AwardedOnSortDescriptor 
    - (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; // If both objects have no awardedOn field, they are in the same "set"
            return NSOrderedDescending; // If the first one has no awardedOn, it is sorted after         
        } 
        if (NULL_OBJECT([object2 valueForKeyPath:[self key]])) { 
            return NSOrderedAscending; // If the second one has no awardedOn, it is sorted after
        } 
        return NSOrderedSame; // If they both have an awardedOn field, they are in the same "set"
    } 
    @end 
    

    This will allow you to have to separate sets: Awesome/Better/Cool and Another/Another One/One More/Yet another, in your example. After that, you should be good with:

    NSSortDescriptor *sortDescriptor1 = [[AwardedOnSortDescriptor alloc] initWithKey:@"awardedOn" ascending:YES];
    NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
    

    On a final note, you might need a litte more work depending on what your “empty” awardedOn fields look like (I assumed, in the code above, that the field was set to null). You can take a look here: https://stackoverflow.com/a/3145789

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

Sidebar

Related Questions

I have this following numpy matrix that I want to sort in ascending order
I have a necessity to sort a given HTML table of the following structure,
I am trying to sort query result into an array. I have following query.
I have the following VB.NET code that I am using to sort a Data.DataTable
I have code like following: switch(sort.Column) { case code: model = (sort.Direction == SortDirection.Ascending)
I have a data with the following format: foo<tab>1.00<space>1.33<space>2.00<tab>3 Now I tried to sort
I have a generic method that looks like the following: -(NSArray *) db_select: (NSString
I have the following code to sort the items in a dropdown list: function
I have the following homework question: Sort the numbers 7 12 8 6 9
I have following data table , with individual sorting ascending and descending buttons. I

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.