Sort NSMutableArray with custom objects by another NSMutableArray [duplicate] - iphone

This question already has answers here:
How do I sort an NSMutableArray with custom objects in it?
(27 answers)
Sorting two NSArrays together side by side
(4 answers)
Closed 9 years ago.
I have 2 NSMutableArrays. First array contains custom object intances with property NSString *itemID, second array contains only from NSString objects with same values of itemID, but in another order. I need to sort first array by itemID property of each object, and it should be sorted like second array.
How I can do this?

guideArray = < YOUR SECOND ARRAY WITH STRING OBJECT >;
unsortedArray = < YOUR FIRST ARRAY WITH CUSTOM OBJECT >;
[unsortedArray sortUsingComparator:^(id o1, id o2) {
Items *item1 = o1;
Items *item2 = o2;
NSInteger idx1 = [guideArray indexOfObject:item1.ItemID];
NSInteger idx2 = [guideArray indexOfObject:item2.ItemID];
return idx1 - idx2;
}];
NSLog(#"%#",unsortedArray);

Store the custom objects in an dictionary with itemID as key, use this dictionary as lookup to sort the objects:
NSArray *objects; // your objects
NSMutableArray *hintArray; // your sorted IDs
NSMutableDictionary *lookupDict = [[NSMutableDictionary alloc] initWithCapacity:[objects count]];
NSMutableArray *sortedObjects = [[NSMutableArray alloc] initWithCapacity:[hintArray count]];
for (id object in objects) {
[lookupDict setValue:object forKey:[object itemID]];
}
for (id hint in hintArray) {
[sortedObjects addObject:[lookupDict valueForKey:hint]];
}
EDIT:
Solution with inplace sort of objects:
NSMutableArray *objects;
NSMutableArray *hintArray;
NSMutableDictionary *lookupDict = [[NSMutableDictionary alloc] initWithCapacity:[hintArray count]];
int i = 0;
for (NSString *itemID in hintArray) {
[lookupDict setValue:[NSNumber numberWithInt:i] forKey:itemID];
i++;
}
[objects sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [[lookupDict valueForKey:[obj1 itemID]] compare:[lookupDict valueForKey:[obj2 itemID]]];
}];

You can compare your two objects using following syntax :-
[items sortUsingComparator:^NSComparisonResult(Attribute *obj1, Attribute *obj2)
{
return [[NSNumber numberWithInt:[stringOrder indexOfObject:obj1.itemID]] compare:[NSNumber numberWithInt:[stringOrder indexOfObject:obj2.itemID]]]
}];
or else you can use following snippet :
NSArray* sortedKeys = [dict keysSortedByValueUsingComparator:^(id obj1, id obj2)
{
return [obj1 compareTo:obj2];
}
Enjoy Programming !

Related

Use existing NSArray object properties to create a new NSArray for sectioned tableView

So I have the kind of classic situation where I want to group my tableView by Month/Year. I have a member of my conference object called beginDateSearchString that I use to put different conference into buckets; my problem is in the next part where I try and fail to use a NSSortDescriptor to sort each bucket by beginDate (which is a date).
I am getting an error related to unsorted not being able to receive sort descriptor type selectors.
Here is the disgusting code:
- (NSArray *)arrayOfDateSortedEvents {
NSMutableArray *sortedArray = [[NSMutableArray alloc] init];
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
//place into buckets
for (WSConference *conference in self.arrayOfEvents) {
if (![dictionary objectForKey:[conference beginDateSearchString]]) {
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:conference,nil];
[dictionary setObject:array forKey:[conference beginDateSearchString]];
}
else {
[[dictionary objectForKey:[conference beginDateSearchString]] addObject:conference];
}
}
//sort each bucket by descriptor beginDate
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:#"beginDate" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:descriptor];
for (NSMutableArray *unsorted in dictionary) {
[unsorted sortUsingDescriptors:sortDescriptors];
}
// now, unkey and add dictionary in order
while ([dictionary count] > 0) {
NSString *lowest = nil;
for (NSMutableArray *array in dictionary) {
if (!lowest)
lowest = [[dictionary allKeysForObject:array] objectAtIndex:0];
else {
if ([(WSConference *)[array objectAtIndex:0] beginDate] < [[dictionary objectForKey:lowest] beginDate])
lowest = [[dictionary allKeysForObject:array] objectAtIndex:0];
}
}
[sortedArray addObject:[dictionary objectForKey:lowest]];
[dictionary removeObjectForKey:lowest];
}
return sortedArray;
}
You want to probably filter the array in addition to sorting. See NSPredicate and the NSArray method -filteredArrayUsingPredicate: Then create an eventsByDateArray of the eventArrays created by the filter. Then in your table view delegate for creating the cells, if everything is ordered properly, the first section would represent the date of the events in the eventArray that is the first object of the eventsByDateArray and the table rows would consist of the events in the eventArray. And so on for each date.
Added
Your fast enumeration is incorrect. You enumerate through the keys of the dictionary. So in your code unsorted equals each of the keys as it enumerates. This is a GREAT lesson to everyone. It does not matter how you 'type' a variable. When Objective-C compiles it turns them all into id. So NSMutableArray *unsorted is not an NSMutableArray unless it is assigned to an NSMutableArray. If you assign unsorted to an NSString it will be an NSString. The fast enumerator for a dictionary works using the keys. So, in this case, unsorted becomes an NSString.
Instead of:
for (NSMutableArray *unsorted in dictionary) {
[unsorted sortUsingDescriptors:sortDescriptors];
}
you should have this:
for (id key in dictionary) {
NSMutableArray *unsorted = [dictionary objectForKey:key];
[unsorted sortUsingDescriptors:sortDescriptors];
}

Order, bucket sort, and order buckets of NSMutableArray

I have an NSArray of Object that has an interesting property that I would like to use in the following way: Given my array of objects with properties:
Object1 - Property A;
Object2 - Property A;
Object3 - Property B;
Object4 - Property D;
Object5 - Property D;
Object6 - Property D
I want these to be bucket sorted by their properties into a new array:
Array1 - Objects Object1, Object2
Array2 - Objects Object3
Array3 - Objects Object 4, Object5, Object6
And then within each array, sort by using a timeStamp property.
I have tried to accomplish this naively by creating a dictionary, adding interesting objects to the dictionary by property like if ([dictionary objectForKey:#"propertyVal"]) //add object else // create array for key, add object to array. This approach has not worked as expected because I end up needing to dekey the NSMutableDictionary using allKeysForValue, which is not reliable.
I feel that this is a fairly common problem and I would love to hear any insight into how I might go about solving this. Code is great, but even an algorithm (with the appropriate objects to use) should suffice.
It's not a proper bucket sort, but should work for a set of three properties. A bit of fiddling and you should be able to adjust it for any number of properties:
Edit. I made a dynamic version (just set property type to what you need):
- (NSMutableArray *)order:(NSDictionary *)objects byProperty:(id)property {
NSMutableSet *propertySet = [NSMutableSet setWithCapacity:5]; // so we can count the unique properties
for (Object *obj in [objects allValues]) {
[propertySet addObject:[obj property]];
}
NSMutableArray *objectCollections = [NSMutableArray arrayWithCapacity:[propertySet count]];
// create arrays for every property
for (int i = 0; i < [objects allValues]; i++) {
NSMutableArray *collection = [NSMutableArray arrayWithCapacity:5];
[objectCollections addObject:collection];
}
NSArray *allProperties = [propertySet allObjects];
// push objects into arrays according to a certain property
for (Object *obj in [dictionary allValues]) {
[[objectCollections objectAtIndex:[allProperties indexOfObject:[obj property]] addObject:obj];
}
NSMutableArray *result = [NSMutableArray arrayWithCapacity:[objectCollections count]];
// sort arrays by timestamp
for (int i = 0; i < [objectCollections count]; i++) {
[result addObject:[[objectCollections objectAtIndex:i] sortedArrayUsingComparator:^(id obj1, id obj2) {
if ([(Object *)obj1 timeStamp] > [(Object *)obj2 timeStamp]) {
return (NSComparisonResult)NSOrderedAscending;
}
if ([(Object *)obj1 timeStamp] < [(Object *)obj2 timeStamp]) {
return (NSComparisonResult)NSOrderedDescending;
}
return (NSComparisonResult)NSOrderedSame;
}];
}
return result;
}

Eliminate double entries in an array and sort the result

I have an app which is retrieving its information from a plist.
The plist is an array of Dictionaries with the keys (author, cat, content).
Now I would like to show the categories in a table.
So I need all unique category entries in a sorted way.
I have successfully implemented following way but I am interested if this is the "right" solution.
Go through plist array and make new NSMutableArray with all category
entries of the dictionary
put this NSMutableArray into a NSSet to get unique Elements
put this NSSet into an NSArray to have the possibility to sort it.
return a NSMutableArray initiated with the sorted NSArray
I feel not , that this is the right way of doing so.
Any suggestions to do it better ?
Thanks a lot!
//InhaltefromWEb is a NSMutableArray
-(NSArray*) getCategories {
NSMutableArray* categorieTemp = [[NSMutableArray alloc] init];
unsigned count = [InhalteFromWeb count];
while (count--) {
NSString *tempString;
tempString=[[InhalteFromWeb objectAtIndex:count] objectForKey:CATEGORY];
NSLog(#"tempString %#", tempString );
[categorieTemp addObject:tempString];
}
NSSet *uniqueElements = [NSSet setWithArray:categorieTemp];
NSLog(#"categories from engine %#", categorieTemp);
NSArray* tempAr = [[[NSArray alloc] initWithArray:[uniqueElements allObjects]]sortedArrayUsingSelector:#selector(caseInsensitiveCompare:)];
return [NSMutableArray arrayWithArray:tempAr];
}
Check for existing String with [categorieTemp containsObject:tempString] and sort it with [categorieTemp sortUsingComparator:...]
Example:
[categorieTemp sortUsingComparator:^NSComparisonResult(id obj1, id obj2)
{
return [(NSString *)obj1 caseInsensitiveCompare:(NSString *)obj2];
}];
I do this to sort my array for a date:
[array sortedArrayUsingComparator: ^(id obj1, id obj2) {
NSDate *date1 = [obj1 objectForKey:#"datum"] == nil ? nilDate : [obj1 objectForKey:#"datum"];
NSDate *date2 = [obj2 objectForKey:#"datum"] == nil ? nilDate : [obj2 objectForKey:#"datum"];
return (NSComparisonResult)[date2 compare: date1];
}];

Objective-C iPhone - Ordering data within multiple sections of a UITableView dataSource

For the purpose of asking this question about ordering. The following MyObject class returns an instance with random generated category names.
I use the following dataSource methods:
numberOfSections accessed with [dataSource count].
titleForSection accessed with [[dataSource objectAtIndex:indexPath.section] valueForKey:#"categoryName"].
numberOfRowsInSection accessed with [[[dataSource objectAtIndex:indexPath.section] valueForKey:#"myObjects"] count].
And finally, the MyObject for each row is accessed with [[[dataSource objectAtIndex:indexPath.section] valueForKey:#"myObjects"] objectAtIndex:indexPath.row] on the cellForRowAtIndexPath method.
I use the following code to create a dataSource that displays 9 section categories, however I'm a little stuck on the ordering of these categories and the data within. Assume there's an NSDate property as part of the MyObject class.
Question: How would I go about using this to display the records in descending order?
- (void)createDatasource
{
NSInteger numberOfObjects = 10;
NSMutableArray *objects = [NSMutableArray arrayWithCapacity:numberOfObjects];
NSMutableArray *categories = [NSMutableArray arrayWithCapacity:numberOfObjects];
for (int i = 0; i < numberOfObjects; i++)
{
MyObject *obj = [[MyObject alloc] init];
[objects addObject:obj];
[categories addObject:obj.category];
[obj release];
}
NSSet *set = [NSSet setWithArray:categories];
NSMutableArray *dataSource = [[NSMutableArray alloc] initWithCapacity:[set count]];
for (NSString *categoryString in set)
{
NSMutableDictionary *mainItem = [[NSMutableDictionary alloc] initWithObjectsAndKeys:nil, #"categoryName", nil, #"myObjects", nil];
NSMutableArray *mainItemMyObjects = [NSMutableArray array];
[mainItem setValue:categoryString forKey:#"categoryName"];
for (MyObject *obj in objects)
{
if ([obj.category isEqualToString:categoryString])
{
[mainItemMyObjects addObject:obj];
}
}
[mainItem setValue:mainItemMyObjects forKey:#"myObjects"];
[dataSource addObject:mainItem];
[mainItem release];
}
NSLog (#"objects = %#\ncategories = %#\nset = %#\ndatasource = %#", objects, categories, set, dataSource);
}
Easiest would be to sort your arrays, using NSMutableArray's sorting mutators or NSArray's sorting methods. Otherwise you'd have to construct some sort of mapping from input indices to dataSource indices for use by the various data source methods.
Edit Requested sample code for sorting, something like this should work. I assume you are wanting to sort everything by a property named date on the MyObject.
// First, sort the myObject mutable array in each category
for (NSDictionary *d in dataSource) {
[[d valueForKey:#"myObjects"] sortUsingComparator:^NSComparisonResult(id o1, id o2){
// Compare dates. NSDate's 'compare:' would do ascending order, so if we just
// reverse the order of comparison they'll come out descending.
return [[o2 date] compare:[o1 date]];
}];
}
// Second, sort the categories by the earliest dated object they contain
[dataSource sortUsingComparator:^NSComparisonResult(id o1, id o2){
// Extract the first object from each category's array, which must be the
// earliest it contains due to the previous sort.
MyObject *myObject1 = [[o1 valueForKey:#"myObjects"] objectAtIndex:0];
MyObject *myObject2 = [[o2 valueForKey:#"myObjects"] objectAtIndex:0];
// Compare dates, as above.
return [[myObject2 date] compare:[myObject1 date]];
}];

How to check if the value in an NSDictionary exists in an array of dictionarys

The title is a bit confusing...I'll explain
I have an NSMutableArray I am populating with NSMutableDictionary objects. What I am trying to do is before the dictionary object is added to the array, I need to check whether any of the dictionaries contain a value equal to an id that is already set.
Example:
Step 1: A button is clicked setting the id of an object for use in establishing a view.
Step 2: Another button is pressed inside said view to save some of its contents into a dictionary, then add said dictionary to an array. But if the established ID already exists as a value to one of the dictionaries keys, do not insert this dictionary.
Here is some code I have that is currently not working:
-(IBAction)addToFavorites:(id)sender{
NSMutableDictionary *fav = [[NSMutableDictionary alloc] init];
[fav setObject:[NSNumber numberWithInt:anObject.anId] forKey:#"id"];
[fav setObject:#"w" forKey:#"cat"];
if ([dataManager.anArray count]==0) { //Nothing exists, so just add it
[dataManager.anArray addObject:fav];
}else {
for (int i=0; i<[dataManager.anArray count]; i++) {
if (![[[dataManager.anArray objectAtIndex:i] objectForKey:#"id"] isEqualToNumber:[NSNumber numberWithInt:anObject.anId]]) {
[dataManager.anArray addObject:fav];
}
}
}
[fav release];
}
One fairly easy way to do this kind of check is to filter the array using an NSPredicate. If there's no match, the result of filtering will be an empty array. So for example:
NSArray *objs = [dataManager anArray];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"id == %#", [NSNumber numberWithInt:i]];
NSArray *matchingObjs = [objs filteredArrayUsingPredicate:predicate];
if ([matchingObjs count] == 0)
{
NSLog(#"No match");
}