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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:18:45+00:00 2026-05-19T02:18:45+00:00

Given a basic key/value array, I’m wanting to store two sorted arrays based on

  • 0

Given a basic key/value array, I’m wanting to store two sorted arrays based on the original array: one array will be sorted by name, and the other by age.

The arrays seem to be sorting correctly when I output them to the log; however, when I try to access them elsewhere in the code, I’m receiving a EXC_BAD_ACCESS error.

Here’s what I have so far:

// MyController.h

@interface MyController : UIViewController {
    NSMutableArray *originalArray;
    NSMutableArray *nameArray;
    NSMutableArray *ageArray;   
}

@property (nonatomic, retain) NSMutableArray *originalArray;
@property (nonatomic, retain) NSMutableArray *nameArray;
@property (nonatomic, retain) NSMutableArray *ageArray;

-(void)someRandomMethod;

@end


// MyController.m

#import "MyController.h"

@implementation MyController

@synthesize originalArray;
@synthesize nameArray;
@synthesize ageArray;

-(void)viewDidLoad {

    // originalArray = (
    //   {
    //     "name" = "Sally";
    //     "age" = 18;
    //   },
    //   {
    //     "name" = "Chad";
    //     "age" = 26;
    //   },
    //   {
    //     "name" = "Carla";
    //     "age" = 24;
    //   },
    // )

    // sort by name
    NSSortDescriptor *sortByNameDescriptor;
    sortByNameDescriptor = [[[NSSortDescriptor alloc]              
                                    initWithKey:@"name"                                                             
                                    ascending:NO] autorelease];
    NSArray *sortByNameDescriptors = [NSArray arrayWithObject:sortByNameDescriptor];
    nameArray = [originalArray sortedArrayUsingDescriptors:sortByNameDescriptors];  

    // sort by age
    NSSortDescriptor *sortByAgeDescriptor;
    sortByAgeDescriptor = [[[NSSortDescriptor alloc]              
                                    initWithKey:@"age"                                                              
                                    ascending:NO] autorelease];
    NSArray *sortAgeDescriptors = [NSArray arrayWithObject:sortByAgeDescriptor];
    ageArray = [originalArray sortedArrayUsingDescriptors:sortByAgeDescriptors];    

    [super viewDidLoad];
}

-(void)someRandomMethod {
    // whenever I try to access the sorted arrays, I receive the EXC_BAD_ACCESS error
    [[nameArray objectAtIndex:0] valueForKey:@"name"];
    [[ageArray objectAtIndex:0] valueForKey:@"age"];
}

-(void)viewDidUnload {
    self.originalArray = nil;
    self.nameArray = nil;
    self.ageArray = nil;
    [super viewDidUnload];
}

- (void)dealloc {
    [originalArray release];
    [nameArray release];
    [ageArray release];
    [super dealloc];
}

@end

Any ideas?

UPDATE: Thanks to @robin, by changing the code above to the code below, everything works great:

// sort by name
NSSortDescriptor *sortByNameDescriptor;
sortByNameDescriptor = [[[NSSortDescriptor alloc]              
                                initWithKey:@"name"                                                             
                                ascending:NO] autorelease];
NSArray *sortByNameDescriptors = [NSArray arrayWithObject:sortByNameDescriptor];
nameArray = [[NSMutableArray alloc] initWithArray:[originalArray sortedArrayUsingDescriptors:sortByNameDescriptors]];  

// sort by age
NSSortDescriptor *sortByAgeDescriptor;
sortByAgeDescriptor = [[[NSSortDescriptor alloc]              
                                initWithKey:@"age"                                                              
                                ascending:NO] autorelease];
NSArray *sortAgeDescriptors = [NSArray arrayWithObject:sortByAgeDescriptor];
ageArray = [[NSMutableArray alloc] initWithArray:[originalArray sortedArrayUsingDescriptors:sortByAgeDescriptors]];    
  • 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-19T02:18:45+00:00Added an answer on May 19, 2026 at 2:18 am

    I dont think you know about this or not but when ever you create an object like string or array or dictionary, with init methods then the retain count gets incremented by 1

    and if you create them like this

    NSArray *anarray = [NSArray arrayWithArray:temp];
    

    this will create an autorelease objects that will be released automatically after sometime.

    So my advice don’t use this type of code if you want to use the objects in more than 1 function. Always use init methods first to get the work done.

    and if you are sure that the objects are not needed for the rest of the program than release them using release methode.

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

Sidebar

Related Questions

I'd like to store a set of key/value pairs in the application settings of
Given a basic grid (like a piece of graph paper), where each cell has
Given a pretty basic source tree structure like the following: trunk ------- QA |--------
A basic definition and example and a few references for X-Macros is given in
Given 2 rgb colors and a rectangular area, I'd like to generate a basic
Given a specific DateTime value, how do I display relative time, like: 2 hours
Given a select with multiple option's in jQuery. $select = $(<select></select>); $select.append(<option>Jason</option>) //Key =
Given A search key --------------------------- | | A | B | C | D
Given a Python object of any kind, is there an easy way to get
Given a DateTime representing a person's birthday, how do I calculate their age in

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.