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

The Archive Base Latest Questions

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

I have an NSMutableArray of custom objects. The custom object (Tag Class) has an

  • 0

I have an NSMutableArray of custom objects. The custom object (Tag Class) has an two properties defined as so:

@interface Tag : NSObject

@property (strong, nonatomic) NSString *tid;
@property (strong, nonatomic) NSString *name;

So, I want to sort my NSMutableArray of these Tag objects by the Tag name – how do I do this?

Also, should I sort this array as it gets loaded or sort it each time I read its contents?

  • 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-28T05:40:55+00:00Added an answer on May 28, 2026 at 5:40 am

    There are several ways to sort a mutable array in objective c.

    The simplest I have found is using the [NSMutableArray sortUsingFunction:] method.

    For your example, something like this should suffice for the sort function

    NSComparisonResult sortTagByName(Tag *tag1, Tag *tag2, void *ignore)
    {
        return [tag1.name compare:tag2.name];
    }
    

    here is a full source listing, that you can use to base your solution on:

    #import <Foundation/Foundation.h>
    
    
    @interface Tag : NSObject {
    
    }
    
    @property (strong, nonatomic) NSString *tid;
    @property (strong, nonatomic) NSString *name;
    
    @end
    
    @implementation Tag 
    @synthesize tid;
    @synthesize name;
        
    
    @end
    
    NSComparisonResult sortTagByName(Tag *tag1, Tag *tag2, void *ignore)
    {
        return [tag1.name compare:tag2.name];
    }
    
    @interface stackExDemo : NSObject
    
    @end
    
    
    @implementation stackExDemo
    
    +(void) demo {
        
        NSMutableArray * array = [NSMutableArray array];
        
        // add your objects here
        
        
        [array sortUsingFunction:sortTagByName context:nil];
    
    }
    
    @end
    

    Breaking this down into it’s relevant components:

    The function

    NSComparisonResult sortTagByName(Tag *tag1, Tag *tag2, void *ignore)
    {
        return [tag1.name compare:tag2.name];
    }
    

    This is simply a plain C function (i.e. not a method, so you don’t need to define it inside an implementation section of any particular class, however it usually makes sense to define it in the class it applies to).

    In the example above, I have defined it after the @end for Tag, simply to make it clear that it it is not a class method.

    The parameter I have named "ignore" is the "context" for the sort. In this case there is no context, so we are going to ignore it’s value.

    Since you are sorting based on an NSString property, NSString’s compare method is convenient, however you can also manually return one of {NSOrderedAscending, NSOrderedSame, NSOrderedDescending}

    Invoking the sort

      [array sortUsingFunction:sortTagByName context:nil];
    

    This repeatedly calls your function for each element in the array, to sort the array.

    If you want to be more specific about the type of sort, there are a number of other methods in NSString that can do the comparison, here is a "cut and paste" from NSString.h which should get you started if you want to research it further.

    - (NSComparisonResult)compare:(NSString *)string;
    - (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask;
    - (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange;
    - (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange locale:(id)locale; // locale arg used to be a dictionary pre-Leopard. We now accepts NSLocale. Assumes the current locale if non-nil and non-NSLocale.
    - (NSComparisonResult)caseInsensitiveCompare:(NSString *)string;
    - (NSComparisonResult)localizedCompare:(NSString *)string;
    - (NSComparisonResult)localizedCaseInsensitiveCompare:(NSString *)string;
    

    In answer to your second question, you would need to do this whenever you have added or moved an element that might cause the array to be unsorted.

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

Sidebar

Related Questions

I have a NSMutableArray that has custom objects in it. Each object does have
I have an NSMutableArray of custom objects called Clip. Each clip Object has some
So I have a custom class Foo that has a number of members: @interface
I have a custom object like: #import <Foundation/Foundation.h> @interface Store : NSObject{ NSString *name;
I have an object that has an NSMutableArray of custom UIViews, a UIImage, and
I have a custom object like: #import <Foundation/Foundation.h> @interface FaxRecipient : NSObject { NSString
I have an NSMutableArray that contains a few custom objects. Two of the objects
I have a custom object called Occasion defined as follows: #import <Foundation/Foundation.h> @interface Occasion
I have a custom class: @interface Player : NSObject { NSInteger mPlayerNo; } -(id)
I have a custom class named Profile and a NSMutableArray which I add the

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.