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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:55:45+00:00 2026-06-14T20:55:45+00:00

I have an array which contains 2 objects. Each object has a type key.

  • 0

I have an array which contains 2 objects. Each object has a “type” key. One of the values for that key is “resource” and the other is “crop”.

I want to sort through that array and create a new array which only contains the objects of the type I need. I thought this code did that…

-(NSArray*)getItemsOfType:(NSString *) type
{
    NSSortDescriptor *sortDescriptor;
    sortDescriptor = [[NSSortDescriptor alloc] initWithKey:type
                                                 ascending:YES];
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
    NSArray *sortedArray;
    sortedArray = [templateObjects sortedArrayUsingDescriptors:sortDescriptors];

    return sortedArray;
}

But all it seems to do is just sort the current array into some kind of order. How can I change that function to do what I need? so I need to use “initWithKey:ascending:selector:” ?

UPDATE

I just tried this code above the code above…

NSPredicate *predicate = [NSPredicate predicateWithFormat: @"templateObjects.type like %@", type];

    NSArray *newArray = [templateObjects filteredArrayUsingPredicate:predicate];

But newArray just appears to be empty? I have a feeling the @”templateObjects.type” part is wrong?

Update 2

ok this code does what I need it to..

-(NSArray*)getItemsOfType:(NSString *) type
{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"type CONTAINS[cd] %@", type];

    NSArray *newArray = [templateObjects filteredArrayUsingPredicate:predicate];

    NSSortDescriptor *sortDescriptor;
    sortDescriptor = [[NSSortDescriptor alloc] initWithKey:type
                                                 ascending:YES];
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
    NSArray *sortedArray;
    sortedArray = [newArray sortedArrayUsingDescriptors:sortDescriptors];

    return sortedArray;
}
  • 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-14T20:55:47+00:00Added an answer on June 14, 2026 at 8:55 pm

    use

    [NSPredicate predicateWithFormat: @"type like %@", type];
    

    instead of

    [NSPredicate predicateWithFormat: @"templateObjects.type like %@", type];
    

    so

    NSPredicate *predicate = [NSPredicate predicateWithFormat: @"type like %@", type];
    

    or

    NSPredicate *predicate = [NSPredicate predicateWithFormat: @"type CONTAINS[cd] %@", type];
    

    This will work as you said both objects have the type key.

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

Sidebar

Related Questions

I have an array ($configOptions) which contains some arrays. Each array ($option) in that
I have a parent class that contains an array of objects each with a
I have an object Results that contains an array of result objects along with
I have two array list. Each has list of Objects of type Employee. The
I have two array list. Each has list of Objects of type Employee. The
I have created an empty json object having an array itemlist(which further contains itemid
I have a data model which contains an array of Product objects. Before reporting
I often have the need to search a javascript array that contains objects. I
I have a Course class which contains collection of CourseItems . Each CourseItem has
I have an array which contains 12 numbers and I want to save this

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.