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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:13:11+00:00 2026-05-17T16:13:11+00:00

I have a NSDictionary collection whose key is a unique id and value is

  • 0

I have a NSDictionary collection whose key is a unique id and value is an array with two different objects (FruitClass, ProductClass) and I would like to group the collection such that it’s sorted first by ProductClass.productName and then by FruitClass.itemName.

So the final list would look something like:

{apple, butter}
{apple, pie}
{banana, daiquiri}
{banana, smoothie}
{melon, zinger}

where the first item is a FruitClass instance item and second is a ProductClass instance item.

What’s the best way to go about doing this? Most of the examples I’ve come across are done on one key. How do you do it with an NSDictionary that has 2 different object types?

Looking at NSDictionary’s’s keysSortedByValueUsingSelector,

- (NSArray *)keysSortedByValueUsingSelector:(SEL)comparator

I get the impression that you would create the ‘compare’ method on the class type of the value object. So for multiple field sort, would I have to resort to creating a new object type, ‘CombinedClass’ which contains FruitClass & ProductClass and implement a ‘compare’ to make this happen?

FruitClass:
{
    NSString *itemName;
}
@end
@interface ProductClass
{
    NSString *productName;
}
@end
  • 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-17T16:13:11+00:00Added an answer on May 17, 2026 at 4:13 pm

    You could add a category to NSArray that would do the comparison and you wouldn’t have to create another class.

    @interface NSArray ( MySortCategory )
        - (NSComparisonResult)customCompareToArray:(NSArray *)arrayToCompare;
    @end
    

    The implementation should be pretty straightforward based on your description.

    Edit I got a little miffed that this was marked down with no comment, so I did a full implementation to make sure it would work. This is a little different than your sample, but the same idea.

    FruitsAndProducts.h

    @interface Fruit : NSObject
    {
        NSString *itemName;
    }
    
    @property(nonatomic, copy)NSString *itemName;
    
    @end
    
    @interface Product : NSObject
    {
        NSString *productName;
    }
    
    @property(nonatomic, copy)NSString *productName;
    
    @end
    

    FruitsAndProducts.m

    #import "FruitsAndProducts.h"
    
    @implementation Fruit
    
    @synthesize itemName;
    
    @end
    
    
    @implementation Product
    
    @synthesize productName;
    
    @end
    

    NSArray+MyCustomSort.h

    @interface NSArray (MyCustomSort)
    - (NSComparisonResult)customCompareToArray:(NSArray *)arrayToCompare;
    @end
    

    NSArray+MyCustomSort.m

    #import "NSArray+MyCustomSort.h"
    
    #import "FruitsAndProducts.h"
    
    @implementation NSArray (MyCustomSort)
    
    - (NSComparisonResult)customCompareToArray:(NSArray *)arrayToCompare
    {
        // This sorts by product first, then fruit.
        Product *myProduct = [self objectAtIndex:0];
        Product *productToCompare = [arrayToCompare objectAtIndex:0];
    
        NSComparisonResult result = [myProduct.productName caseInsensitiveCompare:productToCompare.productName];
        if (result != NSOrderedSame) {
            return result;
        }
    
        Fruit *myFruit = [self objectAtIndex:1];
        Fruit *fruitToCompare = [arrayToCompare objectAtIndex:1];
    
        return [myFruit.itemName caseInsensitiveCompare:fruitToCompare.itemName];
    }
    
    
    @end
    

    Here it is in action

    // Create some fruit.
    Fruit *apple = [[[Fruit alloc] init] autorelease];
    apple.itemName = @"apple";
    Fruit *banana = [[[Fruit alloc] init] autorelease];
    banana.itemName = @"banana";
    Fruit *melon = [[[Fruit alloc] init] autorelease];
    melon.itemName = @"melon";
    
    // Create some products
    Product *butter = [[[Product alloc] init] autorelease];
    butter.productName = @"butter";
    Product *pie = [[[Product alloc] init] autorelease];
    pie.productName = @"pie";
    Product *zinger = [[[Product alloc] init] autorelease];
    zinger.productName = @"zinger";
    
    // create the dictionary. The array has the product first, then the fruit.
    NSDictionary *myDict = [NSDictionary dictionaryWithObjectsAndKeys:[NSArray arrayWithObjects:zinger, banana, nil], @"zinger banana", [NSArray arrayWithObjects:butter, apple, nil], @"butter apple", [NSArray arrayWithObjects:pie, melon, nil], @"pie melon", nil];
    
    NSArray *sortedKeys = [myDict keysSortedByValueUsingSelector:@selector(customCompareToArray:)];
    
    for (id key in sortedKeys) {
        NSLog(@"key: %@", key);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two different NSDictionary objects where there are keys that belong to both
I have some NSDictionary objects stored in an NSArray called telephoneArray . I fetch
the question explains it all. For instance, if I have a NSDictionary like this
i have an NSArray collection. it contains NSArray and NSDictionary. while i iterate the
I have an NSDictionary with two values. Now I want to switch the values.
My Situation: I have an NSDictionary object. Keyed by NSNumber. Values are custom objects.
Say I have: NSDictionary *stuff; // {1 => hi, 2 => bye} NSArray *array
i have an NSDictionary that contain in value and i need to get this
I have an NSDictionary which contains (my custom) GTPerson objects. GTPerson has an NSMutableSet
i have a NSDictionary which looks like: { Item1 = 2.4; Item2 = 5.5;

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.