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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:04:44+00:00 2026-06-16T22:04:44+00:00

I have an NSSet of objects. Each object in the set is an instance

  • 0

I have an NSSet of objects.

Each object in the set is an instance of MyObject with a property called name.

I have another NSArray called nameIndexes which contains name values.

I would like to have a function that takes the NSSet and returns a sorted array sorted using the name property based on its position in the nameIndexes array.

Edit:

Sorry for my misleading, here is an example:

I have a set of MyObject(may not be in the same order):

MyObject1 {name:@"A"}

MyObject2 {name:@"B"}

MyObject3 {name:@"C"}

I have another array of names:

{"B", "A", "C"}

I want an NSArray of:

{MyObject2, MyObject1, MyObject3}
  • 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-16T22:04:46+00:00Added an answer on June 16, 2026 at 10:04 pm
    NSSet *set = //your set.
    
    NSArray *nameIndexes = //the array of names for sorting.
    
    NSArray *result = [[set allObjects] sortedArrayUsingComparator:^NSComparisonResult(MyObject *obj1, MyObject *obj2) {
        int index1 = [nameIndexes indexOfObject:obj1.name];
        int index2 = [nameIndexes indexOfObject:obj2.name];
        return [[NSNumber numberWithInt:index1] compare:[NSNumber numberWithInt:index2]];
    }];
    

    Not 100% certain what you are asking but this will sort take the set of names and turn it into a sorted array sorted based on the index of the names in a second array.

    After edit

    Edit… ok, so your edit completely changed the question. Anyway, from the edit you can just do…

    NSArray *result = [[set allObjects] sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:"name" ascending:YES]]];
    

    This will take the set and return a sorted array sorted by the name property of the objects in alphabetical order.

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

Sidebar

Related Questions

I have the following task: I'm given an NSSet of objects each of which
How to search an NSSet or NSArray for an object which has an specific
I have a Core Data object that contains an NSSet of other objects (e.g.
I have an array of objects (Users) each user has an nsset named devices
So I have two objects, Invoice and InvoiceLineItem. InvoiceLineItem has a property called cost
have written this little class, which generates a UUID every time an object of
Let's say I have an NSSet that contains a collection of objects of type
I have a NSSet of object from the same class and I want to
I have two NSArray s each containing NSString s. I need to test whether
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.