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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:54:40+00:00 2026-05-31T00:54:40+00:00

I have an NSArray of Object that has an interesting property that I would

  • 0

I have an NSArray of Object that has an interesting property that I would like to use in the following way: Given my array of objects with properties:

Object1 - Property A;
Object2 - Property A;
Object3 - Property B;
Object4 - Property D;
Object5 - Property D;
Object6 - Property D

I want these to be bucket sorted by their properties into a new array:

Array1 - Objects Object1, Object2

Array2 - Objects Object3

Array3 - Objects Object 4, Object5, Object6

And then within each array, sort by using a timeStamp property.

I have tried to accomplish this naively by creating a dictionary, adding interesting objects to the dictionary by property like if ([dictionary objectForKey:@"propertyVal"]) //add object else // create array for key, add object to array. This approach has not worked as expected because I end up needing to dekey the NSMutableDictionary using allKeysForValue, which is not reliable.

I feel that this is a fairly common problem and I would love to hear any insight into how I might go about solving this. Code is great, but even an algorithm (with the appropriate objects to use) should suffice.

  • 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-31T00:54:41+00:00Added an answer on May 31, 2026 at 12:54 am

    It’s not a proper bucket sort, but should work for a set of three properties. A bit of fiddling and you should be able to adjust it for any number of properties:

    Edit. I made a dynamic version (just set property type to what you need):

    - (NSMutableArray *)order:(NSDictionary *)objects byProperty:(id)property {
        NSMutableSet *propertySet = [NSMutableSet setWithCapacity:5]; // so we can count the unique properties
        for (Object *obj in [objects allValues]) {
            [propertySet addObject:[obj property]];
        }
    
        NSMutableArray *objectCollections = [NSMutableArray arrayWithCapacity:[propertySet count]];
    
        // create arrays for every property
        for (int i = 0; i < [objects allValues]; i++) {
            NSMutableArray *collection = [NSMutableArray arrayWithCapacity:5];
            [objectCollections addObject:collection];
        }  
        NSArray *allProperties = [propertySet allObjects];
    
        // push objects into arrays according to a certain property
        for (Object *obj in [dictionary allValues]) {
            [[objectCollections objectAtIndex:[allProperties indexOfObject:[obj property]] addObject:obj];
        }
    
        NSMutableArray *result = [NSMutableArray arrayWithCapacity:[objectCollections count]];
        // sort arrays by timestamp
        for (int i = 0; i < [objectCollections count]; i++) {
                [result addObject:[[objectCollections objectAtIndex:i] sortedArrayUsingComparator:^(id obj1, id obj2) {
                if ([(Object *)obj1 timeStamp] > [(Object *)obj2 timeStamp]) {
                    return (NSComparisonResult)NSOrderedAscending;
                }
                if ([(Object *)obj1 timeStamp] < [(Object *)obj2 timeStamp]) {
                    return (NSComparisonResult)NSOrderedDescending;
                }            
                return (NSComparisonResult)NSOrderedSame;
            }];
        }
        return result;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an NSArray of objects. Each object has a property struct LOCATION that
I have an NSArray of NSDictionary objects which I would like to be able
I have NSDate object and I need to find from a NSArray (that has
for an iOS 5.0 application using ARC, i have an NSArray of objects that
I have an NSArray of (Product) objects that are created by parsing an XML
I have been working with NSArray s and NSMutableArray s that store NSDate objects
Example: I have an NSArray with 40 objects. What is the most efficient way
i have an NSArray which contains custom objects, the objects have an NSString property,
I have an object that has an NSMutableArray of custom UIViews, a UIImage, and
I have an 3 object hierarchy which has a root as 'Division' that has

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.