Sorting NSMutableArray By Object's Property - iphone

So this is a rather basic question regarding the best way to sort an NSMutableArray of custom objects.
I have a an NSMutableArray of custom objects, each object with an NSString and NSDate that go together. I need to sort the array by the newest object (so latest NSDate), and I'm pretty sure I could simply use NSDate compare: NSDate if this was an array of just NSDate, but since I need all objects to be sorted and not just the date, I'm not sure if I can use that method.
In terms of pseudo-code, I need to: Look at individual object, determine if the current object's NSDate is the next biggest in the array, and if it is, move the object, not just the date.
Again, this is something I was even hesitant to ask since it's so basic but I don't want to go writing some grossly inefficient method if there is a pre-existing class method that will essentially do what I want, search an array of object's sub properties and sort the objects according to the subproperties.
Thanks for any help.

NSSortDescriptorss make this really simple. With NSMutableArray you can sort the existing array using sortUsingDescriptors: and with immutable arrays you create a new array using sortedArrayUsingDescriptors:
//This will sort by stringProperty ascending, then dateProperty ascending
[mutable_array sortUsingDescriptors:
#[
[NSSortDescriptor sortDescriptorWithKey:#"stringProperty" ascending:YES],
[NSSortDescriptor sortDescriptorWithKey:#"dateProperty" ascending:YES]
]];

This little snippet worked great for me:
[students sortUsingDescriptors:#[[NSSortDescriptor sortDescriptorWithKey:#"name" ascending:YES]]];

Related

NSArray - quick way to find index of object for know description

Hi i have NSArray of id objects.
And i know object description NSString.
how can i find object and avoid array enumeration?
p.s. i don't like to using NSDictionary, bcs it's do code more difficult
If this is a lookup you plan to perform more often and speed is an issue, you should probably not store the objects in a NSArray but instead in a NSDictionary to begin with, where the keys are the description strings of the objects and the values are the objects themselves. You can then use objectForKey: with the description string as argument, which is a O(1) lookup.
Try this one :
[myArray containsObject:yourObject];
Or you can filter it :
[myArray filterUsingPredicate:predicate];

Sorting / Filtering an NSArray / NSMutableArray

More for my interest than anything else.
If you have an Class defined like so...
MyClass
-------
NSString *name
And you put a lot of them into an array (or mutable array). Then you can use a predicate like this...
[NSPredicate predicateWithFormat:#"name = %#", someValue];
to filter the array so that it only contains objects whose names are the value given.
Or sort descriptor like so...
[NSSortDescriptor sortDescriptorWithKey:#"name" ascending:YES];
to sort the array by the name field in ascending order.
My question is, if you have an array of strings (or of NSNumbers) can you use similar "format" predicates?
Say for instance you had the array...
#[#"Cat", #"Bat", #"Dog", #"Cow"];
Could you use a "predicateWithFormat" to filter this array of a "sortDescriptorWithKey" to sort it?
I know you can use blocks but just wondering if this is possible?
Sure, you can filter an array of strings, or anything else, with predicateWithFormat:. As for sorting, you would use sortedArrayUsingSelector:, and use whichever selector you want (compare:, caseInsensitiveCompare:, etc.). There are no keys in a simple array, so you couldn't use sortDescriptorWithKey.
A string does not have any keys to use your predicates on or to sort by. You can find every other possible way to sort an array in the apple docs.

Accessing array/set of objects with keys

I want to be able to add objects to an NSArray and access them with Keys. Currently the way im doing it is creating a seperate NSDictionary of key-value pairs where the value is an integer number representing the index in my NSArray. This seems like an extra step to me.
If my understanding of NSDictionary is correct, only 'values' can be stored: a pointer to an object cannot.
Surely there must be an equivalent NSDictionary type function that allows objects to be stored and accessed with a key? I have looked through the documentation, but cant seem to find any answers, unless im missing something obvious.
NSDictionary is to store key value pairs. if you are adding key value pair after you created the dictioanry, use NSMutableDictionary class . example,
[dictionaryObject setObject:#"" forKey:#"abc"];
You can store objects in NSDictionary and can be accessed via keys...
In short, no.
An array (NSArray) is an ordered collection of references to objects, so simply said, an ordered collection of objects.
As opposed to dictionaries, which are unordered and values are accessed by keys.
You understanding of collections is probably wrong, you don't store values, but pointers (references).
The extra step is necessary if you need to store the references in an array, but in this case, you should consider using a dictionary. An option is to use keys that take care of the order.
For example :
[myDictionary objectForKey:#"1"];
could be an equivalent of :
[myArray objectAtIndex:1];
Thats wrong, you can store objects in a NSDictionary. Look at the method dictionaryWithObjects:forKeys: or dictionaryWithObjectsAndKeys:
I have no experience in Cocoa but looking at the documentation it seems like NSDictionary (or at least NSMutableDictionary) should handle your request (without you using NSArray).
I think I understand your problem. My suggestion for you is to use NSMutableArray and macros, like:
NSMutableArray *array=[[NSMutableArray alloc]init];
#define SOME_MACRO 0
id someObject;
[array insertObject:someObject atIndex:SOME_MACRO];
id getterObject=[array objectAtIndex:SOME_MACRO];
Of course define the macros in the header file.

Can I use instance variables with an NSManagedObject?

So I got entities Level and Tile. Level has a to-many relationship with Tile. Tile has a property 'index'.
Right now I'm using this code to get the tiles array of Level sorted:
- (NSArray *)sortedTiles
{
NSMutableArray *sortedTiles = [NSMutableArray arrayWithArray:[self.tiles allObjects]];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"index" ascending:YES];
[sortedTiles sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[sortDescriptor release];
return sortedTiles;
}
This works, but I want to be able to retrieve a single Tile with a certain index, so I wrote this method in Level.h:
- (Tile *)tileWithIndex:(NSInteger)index;
The implementation is fairly simple:
- (Tile *)tileWithIndex:(NSInteger)index
{
NSArray *sortedTiles = [self sortedTiles];
Tile *tile = [sortedTiles objectAtIndex:index];
return tile;
}
Now, ofcourse this isn't the most efficient way in doing so because the tiles array has to be allocated and sorted each time, so I was thinking: if I just add an instance variable to Level, 'sortedTiles', then I won't have to rebuild it each time. But Level is a subclass of NSManagedObject, so is this possible and/or wise to do?
I wouldn't. The NSManagedObjects are a reflection of a record in the database and adding additional member variables outside the model strikes me as adding something that doesn't belong.
There are other, better ways to accomplish the same thing. The simplest would be to get the controller or delegate that's fetching all these objects to fetch, sort and retain the array locally.
For large or complex situations, you can use an NSFetchRequestController to collect, sort and dole out NSManagedObjects as needed. This integrates nicely into a UITableViewController. I haven't done any performance testing, but for a situation where there are potentially a large number of records, I would try this first to see if the Fetch Results classes own cache management is sufficient.

iPhone - How to build a list of unique items

I want an array which contains only unique items. I know I could do this with an NSDictionary adding items with keys and then get allKeys. This would ensure that the NSArray contains only unique items, but I feel that this would be overkill and believe that there should be an easier way to do this, but cannot find one.
NSArray* uniqueArray = [[NSSet setWithArray:originalArray] allObjects];
Uniqueness is based on the isEqual: method.
Use NSSet or NSMutableSet for this. Keep in mind that uniqueness will be based on object address if you don't override the isEqual: method. Unless, of course, you are using classes that implement that method (NSNumber, NSValue, for example).
You can use NSSet for this