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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:46:00+00:00 2026-06-09T04:46:00+00:00

I am trying to sort a mutableArray of NSDictionaries, currently I can do this

  • 0

I am trying to sort a mutableArray of NSDictionaries, currently I can do this by sorting with one objectForKey value, but the problem with this is that alot of these values can be the same so I need to have the other objectForKey to sort the array. I was wondering how, if the first objectForKey comparison returns an identical value, how I could then move onto a second objectForKey comparison and so on and so forth..

Currently I am doing this, which works.. but obviously only on the one key.

[dataArrayOfDictionaries sortUsingComparator:^ NSComparisonResult(NSDictionary *d1, NSDictionary *d2)
    {
        NSString *n1 = [d1 objectForKey:@"DESCRIPTION"];
        NSString *n2 = [d2 objectForKey:@"DESCRIPTION"];
        return [n1 localizedCompare:n2];
    } ];

In my investigation I have tried this –

[dataArrayOfDictionaries sortUsingComparator:^ NSComparisonResult(NSDictionary *d1, NSDictionary *d2)
{
    NSString *n1 = [d1 objectForKey:@"DESCRIPTION"];
    NSString *n2 = [d2 objectForKey:@"DESCRIPTION"];
    return [n1 localizedCompare:n2];
    n1 = [d1 objectForKey:@"ID"];
    n2 = [d2 objectForKey:@"ID"];
    return [n1 localizedCompare:n2];

} ];

However this is not working as it sorts on description then completely resorted based off ID, I am wanting it if Description keys are the same to then sort those values by the ID key.. if that doesn’t work then go down the list of Keys until its sorted.. I hope this makes sense.. any help would be highly appreciated.

  • 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-09T04:46:01+00:00Added an answer on June 9, 2026 at 4:46 am

    You pretty much described the solution in your question: “if Description keys are the same to then sort those values by the ID key”. You can translate that English sentence directly into code:

    [dataArrayOfDictionaries sortUsingComparator:^ NSComparisonResult(NSDictionary *d1, NSDictionary *d2) {
        NSString *n1 = [d1 objectForKey:@"DESCRIPTION"];
        NSString *n2 = [d2 objectForKey:@"DESCRIPTION"];
        NSComparisonResult result = [n1 localizedCompare:n2];
        if (result == NSOrderedSame) {
            n1 = [d1 objectForKey:@"ID"];
            n2 = [d2 objectForKey:@"ID"];
            result = [n1 localizedCompare:n2];
        }
        return result;
    }];
    

    You could also perform the sort using two NSSortDescriptor objects:

    [dataArrayOfDictionaries sortUsingDescriptors:[NSArray arrayWithObjects:
        [NSSortDescriptor sortDescriptorWithKey:@"DESCRIPTION" ascending:YES
            selector:@selector(localizedCompare:)],
        [NSSortDescriptor sortDescriptorWithKey:@"ID" ascending:YES
            selector:@selector(localizedCompare:)],
        nil]];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to sort column Foo that has one Bar . Currently, I'm using
I'm trying to sort this object by the score value so I can populate
Been trying to sort this one out for a while. I'd really appreciate any
I'm trying to sort an array that looks like this: var dateGroups = [
Trying to sort this out... I need a query that will combine both tables
I was trying to sort some xml with jquery, but this code doesn't affect
I have been trying to sort this problem out for the last few days.
Hello Trying to sort this list by Month and day. Can not get it
Trying to sort this array of objects according to (1) depth and (2) weight,
I am trying to sort a two-dimensional array by a timestamp column Descending. 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.