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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:27:50+00:00 2026-06-18T01:27:50+00:00

Is there a way to rearrange an array of objects such that the contents

  • 0

Is there a way to rearrange an array of objects such that the contents are rearranged such that all the xth elements are grouped in another array? I’m not sure the best way to explain this so I’ll illustrate with an example:

theArray has 5 properties: number, color, size, height, quality. There are 50 instances of this array, each with different properties.

I want to rearrange this so there are 5 arrays with 50 items inside each array.

The way I would do it is:

NSMutableArray *innerTemp = [[NSMutableArray alloc] initWithCapacity:49];
NSMutableArray *outerTemp = [[NSMutableArray alloc] initWithCapacity:4];

for (int i = 0 ; i < [theArray count]; i++){
    [innerTemp addObject:[[theArray objectAtIndex:i] number]];
    [innerTemp addObject:[[theArray objectAtIndex:i] color]];
    [innerTemp addObject:[[theArray objectAtIndex:i] height]];
    [innerTemp addObject:[[theArray objectAtIndex:i] age]];
    [innerTemp addObject:[[theArray objectAtIndex:i] quality]];

    [outerTemp addObject:temp];
}

So I guess my question is, is there some easier way/more efficient to do this?

  • 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-18T01:27:51+00:00Added an answer on June 18, 2026 at 1:27 am

    Just use KVC (Key Value Coding):

    Assuming your class looks something like this:

    @interface InstanceClass : NSObject
    
    // IIRC properties need to be objects (hence the NSValue around NSSize), there is no auto-wrapping.
    @property (readwrite, strong, nonatomic) NSNumber *number;
    @property (readwrite, strong, nonatomic) NSColor *color;
    @property (readwrite, strong, nonatomic) NSValue *sizeValue;
    @property (readwrite, strong, nonatomic) NSNumber *height;
    @property (readwrite, strong, nonatomic) NSNumber *quality;
    
    @end
    

    and you have an array like

    NSArray *instances = @[instance1, instance2, instance3, instance4, ...];
    

    then this would be all you need:

    NSArray *numbers   = [instances valueForKey:@"number"];  // array of numbers.
    NSArray *colors    = [instances valueForKey:@"color"];   // array of colors.
    NSArray *sizes     = [instances valueForKey:@"size"];    // array of sizes.
    NSArray *heights   = [instances valueForKey:@"height"];  // array of heights.
    NSArray *qualities = [instances valueForKey:@"quality"]; // array of qualities.
    

    This will do the trick.

    Assuming your items in the instances array are numbered from 1..n a call to [instances valueForKey:@"number"] would then return an NSArray like @[@1, @2, @3, @4, ...].

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

Sidebar

Related Questions

Is there a way to tell Visual Studio 2005 to not rearrange your SSIS
Is there any way that things (e.g. methods) in javadoc don't get rearranged alphabetically?
For Array, there is a pretty sort method to rearrange the sequence of elements.
Is there way that I can read the file from remote server using fopen
Is there any way in Notepad++ (or even with another tool) to change the
I have a line in a javascript function that sort an array of objects
Is there a convenient way to rearrange how multiple displays are positioned via C#?
Is there any way in gVim to rearrange tabs by dragging and dropping them
What is the best way to rearrange elements in an list? I need the
Is there way to get file from windows xp command prompt? I tried to

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.