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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:17:55+00:00 2026-06-13T00:17:55+00:00

I have an custom object which contains time period based information, for instance the

  • 0

I have an custom object which contains time period based information, for instance the attributes endCalYear, endMonth and periodLength which indicate the end of each period and its length.

I would like to create an NSSortDescriptor based or other sorting method which combines these three attributes and allows sorting this object on all three keys at the same time.

Example:

EndCalYear  endMonth  periodLength (months) sortOrder
2012        6         6                     1
2012        6         3                     2
2012        3         3                     3
2011        12        12                    4

The sort algorithm would be completely discretionary based on my own algorithm.

How could I code such an algorithm?

The block based sortDescriptorWithKey:ascending:comparator: method won’t work in my view because it would allow me to specify only one sorting key. However, I need to sort on all three keys at the same time.

Any ideas or thoughts on how to solve this?

Thank you!

  • 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-13T00:17:55+00:00Added an answer on June 13, 2026 at 12:17 am

    You could sort with a block instead:

    NSArray *sortedArray;
    sortedArray = [myArray sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
        MyObject *first = (MyObject*)a;
        MyObject *second = (MyObject*)b;
    
        if (first.endCalYear < second.endCalYear) {
            return NSOrderedAscending;
        }
        else if (first.endCalYear > second.endCalYear) {
            return NSOrderedDescending;
        }
        // endCalYear is the same
    
        if (first.endMonth < second.endMonth) {
            return NSOrderedAscending;
        }
        else if (first.endMonth > second.endMonth) {
            return NSOrderedDescending;
        }    
        // endMonth is the same
    
        if (first.periodLength < second.periodLength) {
            return NSOrderedAscending;
        }
        else if (first.periodLength > second.periodLength) {
            return NSOrderedDescending;
        }
        // periodLength is the same
    
        return NSOrderedSame;
    }]
    

    This sorts by endCalYear ascending, then endMonth ascending and finally periodLength ascending. You could modify it to change the order or switch the signs in the if statement to make it descending.

    For NSFetchedResultsController you might want to try something else:

    It looks like you can pass it a list of descriptors, one for each column that you want sorted:

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSSortDescriptor *descriptor1 = [[NSSortDescriptor alloc] initWithKey:@"endCalYear" ascending:YES];
    NSSortDescriptor *descriptor2 = [[NSSortDescriptor alloc] initWithKey:@"endMonth" ascending:YES];
    NSSortDescriptor *descriptor3 = [[NSSortDescriptor alloc] initWithKey:@"periodLength" ascending:YES];
    NSArray *sortDescriptors = @[descriptor1, descriptor2, descriptor3];
    [fetchRequest setSortDescriptors:sortDescriptors];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a single NSDictionary object which contains a large number of custom objects.
I have a custom tableview cell, which contains some object UIImageView, UILabel, and a
i have an NSArray which contains custom objects, the objects have an NSString property,
Say I have a list of member, each of which is a custom object:
I have a custom asp.net control which contains some checkBoxes. I know how to
I have a Day object which contains some statistics and is updated daily. Here's
I have a page which contains a dynamic number of custom WebControls. What I
I created a custom control which contains an Image control. I'd like to have
In my application i have an custom NSObject, which contains 2 mutable Arrays. I
I have an NSOperationQueue to which i have added my custom class's objects(inherits from

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.