Nesting means taking an array of key value pairs and grouping them hierarchically by a specified key. See this page for examples: http://bl.ocks.org/d/3176159/. If not, I'll just try to port https://github.com/mbostock/d3/blob/master/src/core/nest.js over but I don't want to reinvent the wheel.
This is the answer that I came up with. Let me know if you have suggestions for improvements.
// Wrapper method
// keys are in order of hierarchy
- (NSMutableArray *)nestArray:(NSArray *)array withKeys:(NSArray *)keys
{
return [self nestArray:array withKeys:keys depth:0];
}
// Private
// Assumes arrays of dictionaries with strings as the entries.
- (NSMutableArray *)nestArray:(NSArray *)array withKeys:(NSArray *)keys depth:(int)depth
{
// Current key
NSString *key = [keys objectAtIndex:depth];
depth++;
// Create dictionary of the keys
NSMutableDictionary *map = [[NSMutableDictionary alloc] init];
for (NSDictionary *dictionary in array) {
NSString *value = [dictionary objectForKey:key];
if ([map objectForKey:value]) {
[[map objectForKey:value] addObject:dictionary];
} else {
[map setObject:[NSMutableArray arrayWithObject:dictionary] forKey:value];
}
}
NSMutableArray *nest = [[NSMutableArray alloc] init];
for (NSString *valkey in [map allKeys]) {
NSMutableArray *values = [map objectForKey:valkey];
if (depth < keys.count) {
values = [self nestArray:[NSArray arrayWithArray:array] withKeys:keys depth:depth];
}
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:valkey,#"key",values,#"values", nil];
[nest addObject:dictionary];
}
return nest;
}
Related
I have an NSMutableArray that contains several NSMutableDictionary object, each dict has a NSString and an NSMutableArray in it.
My problem is that I need to find the correct dict based on a string match and when found insert an object into the NSMutableArray in the same dict where the string match was found, I can only make this work the first time when the array is empty because after that I am unable to match the correct string:
Here is what I have tried so far, I tried using contains object but that wont get me inside the dict inside the array so I am stuck
NSMutableArray *variantOptions = [[NSMutableArray alloc] init];
for (NSDictionary *variant in variantInfo) {
NSMutableDictionary *variantOption = [[NSMutableDictionary alloc] init];
NSMutableArray *variantOptionValues = [[NSMutableArray alloc] init];
NSString *name = variant[#"name"];
NSString *value = variant[#"value"];
if(variantOptions.count > 0) {
// Need to loop through the array until name isEquaToString variantOptionName and when true insert value into variantOptionValuesArray in that same dict and if not exists create a new dict and insert in array
} else {
[variantOptionValues addObject:value];
[variantOption setObject:name forKey:#"variantOptionName"];
[variantOption setObject:variantOptionValues forKey:#"variantOptionValues"];
}
[variantOptions addObject:variantOption];
}
for (NSDictionary *variant in variantInfo) {
NSString *name = variant[#"name"];
NSString *value = variant[#"value"];
BOOL found = false;
for (NSMutableDictionary *v in variantOptions) {
if (v[#"name"] isequalToString:name]) {
NSMutableArray *values = v[#"variantOptionValues"];
[values addObject:value];
found = true;
break;
}
}
if (!found) {
// name was not found
NSMutableDictionary *variantOption = [[NSMutableDictionary alloc] init];
NSMutableArray *variantOptionValues = [NSMutableArray alloc] init];
[variantOptionValues addObject:value];
[variantOption setObject:name forKey:#"variantOptionName"];
[variantOption setObject:variantOptionValues forKey:#"variantOptionValues"];
[variantOptions addObject:variantOption];
}
}
I'm having difficulty figuring out how to incorporate a counter so the "setObject" increments it's 'Save' in this for..loop.
NSMutableArray *NewArray = [NSMutableArray new];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
for ( DBFILESMetadata *entry in result.entries) {
[dict setObject:entry.pathDisplay forKey:#"pathDisplay"];
[dict setObject:entry.name forKey:#"name"];
[NewArray addObject:dict];
}
I'm sure this is an easy answer,the last line is only saving the last item of result.entries. The NewArray has the correct count of items, but every item in the array is the last item of result.entries:
2017-04-13 16:47:58.876 Sites[11145:688352] NewArray (
{
name = 229;
pathDisplay = "/Sites/229";
},
{
name = 229;
pathDisplay = "/Sites/229";
}
).
I need to add a counter of some type to set the next object, just confused on where it should go.
Thanks in advance.
I figured this out:
for ( DBFILESMetadata *entry in result.entries) {
[imagePaths addObject:entry.pathDisplay];
[names addObject:entry.name];
}
for(int i=0; i<[result.entries count]; i++) {
dict = #{#"name": names[i], #"pathDisplay": imagePaths[i]};
[allObjects addObject:dict];
}
The dictionary item keys must be different. Otherwise, you see always last set item. Because the dictionary will overide when the save same key. So you can use just like below.
1.Solutions
NSMutableArray *NewArray = [NSMutableArray new];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
int i = 0;
for ( DBFILESMetadata *entry in result.entries) {
[dict setObject:entry.pathDisplay forKey:[NSString stringWithFormat:#"pathDisplay%d",i]];
[dict setObject:entry.name forKey:[NSString stringWithFormat:#"name%d",i]];
[NewArray addObject:dict];
i++;
}
2.Solution
Create a class that will hold the json property, just like a below.
HoldJsonDataClass.h
#interface HoldJsonDataClass : JsonData
#property (nonatomic,retain) NSString *name;
#property (nonatomic,retain) NSString * pathDisplay;
#end
HoldJsonDataClass.m
#implementation HoldJsonDataClass
#end
And than fill the class to the array. This solution is more clear.
for ( DBFILESMetadata *entry in result.entries) {
HoldJsonDataClass *holdJsonClass = [HoldJsonDataClass new];
holdJsonClass.name = entry.name;
holdJsonClass.pathDisplay = entry.pathDisplay;
[NewArray addObject:holdJsonClass];
}
This question already has answers here:
NSDictionary with ordered keys
(9 answers)
Closed 6 years ago.
I am passing NSDictionary to my function as a parameter. I want it's key and values to be in order as I inserted.
for eg. expected output is:
mutable dict:{
zKey1 = Value1;
fKey2 = Value2;
aKey3 = Value3;
}
I have tried following ways to create and set value for keys.
NSMutableDictionary *mutableDict = [[NSMutableDictionary alloc]init];
[mutableDict setObject:#"Value1" forKey:#"zKey1"];
[mutableDict setObject:#"Value2" forKey:#"fKey2"];
[mutableDict setObject:#"Value3" forKey:#"aKey3"];
NSMutableDictionary *dic2=[[NSMutableDictionary alloc]initWithObjectsAndKeys:#"1004",#"userId",#"cucumber",#"domain",#"168d5c02f ",#"token",#"1004",#"userId1",#"cucumber",#"domain1",#"168d5c02f ",#"token1", nil];
NSDictionary * dict = [NSDictionary
dictionaryWithObjects:#[#"Ravi",#"33",#"India",#"India"]
forKeys:#[#"name",#"age",#"location",#"country"]];
NSArray *sortedKeys = [[dict allKeys] sortedArrayUsingSelector: #selector(compare:)];
NSMutableArray *sortedValues = [NSMutableArray array];
for (NSString *key in sortedKeys) {
[sortedValues addObject: [dict objectForKey: key]];
}
NSString *obj1=#"1004";
NSString *obj2=#"cucumber";
NSString *obj3=#"168d5c02f";
NSString *key1=#" userId";
NSString *key2=#"domain";
NSString *key3=#"token";
NSLog(#"dict %#",dict);
NSDictionary *dict8 =[NSDictionary
dictionaryWithObjects:#[obj1,obj2,obj3]
forKeys:#[key1,key2,key3]];
But nothing has worked I am always getting output as
mutable dict:{
aKey3 = Value3;
fKey2 = Value2;
zKey1 = Value1;
}
dict8 {
domain = cucumber;
token = 168d5c02f;
userId = 1004;
}
dict {
age = 33;
country = India;
location = India;
name = Ravi;
}
dic= {
domain = cucumber;
domain1 = cucumber;
token = "168d5c02f ";
token1 = "168d5c02f ";
userId = 1004;
userId1 = 1004;
}
It is always sorting values according to alphabetical order of keys. Many people say that NSDictionary is an unsorted container. But it does gets sorted. Need help desperately. Thank you in advance.
NSDictionary is not ordered by default. It will always be without any order. To create an ordered dictionary, you will need to override the existing form of the Data structure. You can read This tutorial to achieve your end.
To summarize the tutorial (Because everyone hates link-only answers and links can die any time):
NSDictionary stores its keys in a hash table, which is unordered by design. Since this lack of order is fundamental to the hash table storeage, you have to perform subclassing of NSMutableDictionary (and hence reimplementation of the storage).
In your .h file
//
// OrderedDictionary.h
// OrderedDictionary
//
// Created by Matt Gallagher on 19/12/08.
// Copyright 2008 Matt Gallagher. All rights reserved.
//
// Permission is given to use this source code file without charge in any
// project, commercial or otherwise, entirely at your risk, with the condition
// that any redistribution (in part or whole) of source code must retain
// this copyright and permission notice. Attribution in compiled projects is
// appreciated but not required.
//
#import <Cocoa/Cocoa.h>
#interface OrderedDictionary : NSMutableDictionary
{
NSMutableDictionary *dictionary;
NSMutableArray *array;
}
- (void)insertObject:(id)anObject forKey:(id)aKey atIndex:(NSUInteger)anIndex;
- (id)keyAtIndex:(NSUInteger)anIndex;
- (NSEnumerator *)reverseKeyEnumerator;
#end
In your .m file:
//
// OrderedDictionary.m
// OrderedDictionary
//
// Created by Matt Gallagher on 19/12/08.
// Copyright 2008 Matt Gallagher. All rights reserved.
//
// Permission is given to use this source code file without charge in any
// project, commercial or otherwise, entirely at your risk, with the condition
// that any redistribution (in part or whole) of source code must retain
// this copyright and permission notice. Attribution in compiled projects is
// appreciated but not required.
//
#import "OrderedDictionary.h"
NSString *DescriptionForObject(NSObject *object, id locale, NSUInteger indent)
{
NSString *objectString;
if ([object isKindOfClass:[NSString class]])
{
objectString = (NSString *)[[object retain] autorelease];
}
else if ([object respondsToSelector:#selector(descriptionWithLocale:indent:)])
{
objectString = [(NSDictionary *)object descriptionWithLocale:locale indent:indent];
}
else if ([object respondsToSelector:#selector(descriptionWithLocale:)])
{
objectString = [(NSSet *)object descriptionWithLocale:locale];
}
else
{
objectString = [object description];
}
return objectString;
}
#implementation OrderedDictionary
- (id)init
{
return [self initWithCapacity:0];
}
- (id)initWithCapacity:(NSUInteger)capacity
{
self = [super init];
if (self != nil)
{
dictionary = [[NSMutableDictionary alloc] initWithCapacity:capacity];
array = [[NSMutableArray alloc] initWithCapacity:capacity];
}
return self;
}
- (void)dealloc
{
//This method is pre-ARC. Manual Release commands don't work now.
//[dictionary release];
//[array release];
//[super dealloc];
}
- (id)copy
{
return [self mutableCopy];
}
- (void)setObject:(id)anObject forKey:(id)aKey
{
if (![dictionary objectForKey:aKey])
{
[array addObject:aKey];
}
[dictionary setObject:anObject forKey:aKey];
}
- (void)removeObjectForKey:(id)aKey
{
[dictionary removeObjectForKey:aKey];
[array removeObject:aKey];
}
- (NSUInteger)count
{
return [dictionary count];
}
- (id)objectForKey:(id)aKey
{
return [dictionary objectForKey:aKey];
}
- (NSEnumerator *)keyEnumerator
{
return [array objectEnumerator];
}
- (NSEnumerator *)reverseKeyEnumerator
{
return [array reverseObjectEnumerator];
}
- (void)insertObject:(id)anObject forKey:(id)aKey atIndex:(NSUInteger)anIndex
{
if (![dictionary objectForKey:aKey])
{
[self removeObjectForKey:aKey];
}
[array insertObject:aKey atIndex:anIndex];
[dictionary setObject:anObject forKey:aKey];
}
- (id)keyAtIndex:(NSUInteger)anIndex
{
return [array objectAtIndex:anIndex];
}
- (NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level
{
NSMutableString *indentString = [NSMutableString string];
NSUInteger i, count = level;
for (i = 0; i < count; i++)
{
[indentString appendFormat:#" "];
}
NSMutableString *description = [NSMutableString string];
[description appendFormat:#"%#{\n", indentString];
for (NSObject *key in self)
{
[description appendFormat:#"%# %# = %#;\n",
indentString,
DescriptionForObject(key, locale, level),
DescriptionForObject([self objectForKey:key], locale, level)];
}
[description appendFormat:#"%#}\n", indentString];
return description;
}
#end
You can Download Matt Gallagher's orderedDictionary here.
I have a horizontally and vertically scrollable table. I get the data for the header and first column from the web service(json). I want to sort the data in ascending order and remove duplicate data from both header and the first column. For removing duplicate values I used the following code:
-(void) requestFinished: (ASIHTTPRequest *) request
{
NSString *theJSON = [request responseString];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSMutableArray *jsonDictionary = [parser objectWithString:theJSON error:nil];
headData = [[NSMutableArray alloc] init];
NSMutableArray *head = [[NSMutableArray alloc] init];
leftTableData = [[NSMutableArray alloc] init];
NSMutableArray *left = [[NSMutableArray alloc] init];
rightTableData = [[NSMutableArray alloc]init];
for (NSMutableArray *dictionary in jsonDictionary)
{
Model *model = [[Model alloc]init];
model.cid = [[dictionary valueForKey:#"cid"]intValue];
model.iid = [[dictionary valueForKey:#"iid"]intValue];
model.yr = [[dictionary valueForKey:#"yr"]intValue];
model.val = [dictionary valueForKey:#"val"];
[mainTableData addObject:model];
[head addObject:[NSString stringWithFormat:#"%ld", model.yr]];
[left addObject:[NSString stringWithFormat:#"%ld", model.iid]];
}
NSOrderedSet *orderedSet = [NSOrderedSet orderedSetWithArray:head];
headData = [[orderedSet array] mutableCopy];
// NSSet *set = [NSSet setWithArray:left];
// NSArray *array2 = [set allObjects];
// NSLog(#"%#", array2);
NSOrderedSet *orderedSet1 = [NSOrderedSet orderedSetWithArray:left];
NSMutableArray *arrLeft = [[orderedSet1 array] mutableCopy];
//remove duplicate enteries from header array
[leftTableData addObject:arrLeft];
NSMutableArray *right = [[NSMutableArray alloc]init];
for (int i = 0; i < arrLeft.count; i++)
{
NSMutableArray *array = [[NSMutableArray alloc] init];
for (int j = 0; j < headData.count; j++)
{
/* NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF.iid == %ld", [[arrLeft objectAtIndex:i] intValue]];
NSArray *filteredArray = [mainTableData filteredArrayUsingPredicate:predicate];*/
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF.iid == %ld AND SELF.yr == %ld", [[arrLeft objectAtIndex:i] intValue], [[headData objectAtIndex:j] intValue]];
NSArray *filteredArray = [mainTableData filteredArrayUsingPredicate:predicate];
if([filteredArray count]>0)
{
Model *model = [filteredArray objectAtIndex:0];
[array addObject:model.val];
}
}
[right addObject:array];
}
[rightTableData addObject:right];
}
How will I sort the arrays in ascending order?
Please help.
OK, so you have a model object that looks something like this...
#interface Model: NSObject
#property NSNumber *idNumber;
#property NSNumber *year;
#property NSString *value;
#end
Note, I am intentionally using NSNumber and not NSInteger for reasons that will become clear.
At the moment you are trying to do a lot all in one place. Don't do this.
Create a new object to store this data. You can then add methods to get the data you need. Seeing as you are displaying in a table view sectioned by year and then each section ordered by idNumber then I'd do something like this...
#interface ObjectStore: NSObject
- (void)addModelObject:(Model *)model;
// standard table information
- (NSInteger)numberOfYears;
- (NSInteger)numberOfIdsForSection:(NSinteger)section;
// convenience methods
- (NSNumber *)yearForSection:(NSInteger)section;
- (NSNumber *)idNumberForSection:(NSInteger)section row:(NSInteger)row;
- (NSArray *)modelsForSection:(NSInteger)section row:(NSInteger)row;
// now you need a way to add objects
- (void)addModelObject:(Model *)model;
#end
Now to implement it.
We are going to store everything in one dictionary. The keys will be years and the objects will be dictionaries. In these dictionaries the keys will be idNumbers and the objects will be arrays. These array will hold the models.
So like this...
{
2010 : {
1 : [a, b, c],
3 : [c, d, e]
},
2013 : {
1 : [g, h, u],
2 : [e, j, s]
}
}
We'll do this with all the convenience methods also.
#interface ObjectStore: NSObject
#property NSMutableDictionary *objectDictionary;
#end
#implementation ObjectStore
+ (instancetype)init
{
self = [super init];
if (self) {
self.objectDictionary = [NSMutableDictionary dictionary];
}
return self;
}
+ (NSInteger)numberOfYears
{
return self.objectDictionary.count;
}
+ (NSInteger)numberOfIdsForSection:(NSinteger)section
{
// we need to get the year for this section in order of the years.
// lets create a method to do that for us.
NSNumber *year = [self yearForSection:section];
NSDictionary *idsForYear = self.objectDictionary[year];
return idsForYear.count;
}
- (NSNumber *)yearForSection:(NSInteger)section
{
// get all the years and sort them in order
NSArray *years = [[self.obejctDictionary allKeys] sortedArrayUsingSelector:#selector(compare:)];
// return the correct year
return years[section];
}
- (NSNumber *)idNumberForSection:(NSInteger)section row:(NSInteger)row
{
// same as the year function but for id
NSNumber *year = [self yearForSection:section];
NSArray *idNumbers = [[self.objectDictionary allKeys]sortedArrayUsingSelector:#selector(compare:)];
return idNumbers[row];
}
- (NSArray *)modelsForSection:(NSInteger)section row:(NSInteger)row
{
NSNumber *year = [self yearForSection:section];
NSNumber *idNumber = [self idForSection:section row:row];
return self.objectDictionary[year][idNumber];
}
// now we need a way to add objects that will put them into the correct place.
- (void)addModelObject:(Model *)model
{
NSNumber *modelYear = model.year;
NSNumber *modelId = model.idNumber;
// get the correct storage location out of the object dictionary
NSMutableDictionary *idDictionary = [self.objectDictionary[modelYear] mutableCopy];
// there is a better way to do this but can't think atm
if (!idDictionary) {
idDictionary = [NSMutableDictionary dictionary];
}
NSMutableArray *modelArray = [idDictionary[modelId] mutableCopy];
if (!modelArray) {
modelArray = [NSMutableArray array];
}
// insert the model in the correct place.
[modelArray addObject:model];
idDictionary[modelId] = modelArray;
self.objectDictionary[modelYear] = idDictionary;
}
#end
With all this set up you can now replace your complex function with this...
-(void) requestFinished: (ASIHTTPRequest *) request
{
NSString *theJSON = [request responseString];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *jsonDictionary = [parser objectWithString:theJSON error:nil];
for (NSDictionary *dictionary in jsonDictionary)
{
Model *model = [[Model alloc]init];
model.cid = [dictionary valueForKey:#"cid"];
model.idNumber = [dictionary valueForKey:#"iid"];
model.year = [dictionary valueForKey:#"yr"];
model.val = [dictionary valueForKey:#"val"];
[self.objectStore addModelObject:model];
}
}
To get the models out for a particular row then just use...
[self.objectStore modelsForSection:indexPath.section row:indexPath.row];
To get the number of sections in the tableview delegate method...
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.objectStore numberOfYears];
}
No messing around with the model in the view controller.
Welcome to the MVC pattern.
There's a crap ton of code here but by placing all the code here you can remove all the complex code from your VC.
NSSet keeps only non-duplicate objects within themselves so to keep only unique objects in array you can use NSSet as -
Suppose you have array with duplicate objects
NSArray *arrayA = #[#"a", #"b", #"a", #"c", #"a"];
NSLog(#"arrayA is: %#", arrayA);
//create a set with the objects from above array as
//the set will not contain the duplicate objects from above array
NSSet *set = [NSSet setWithArray: arrayA];
// create another array from the objects of the set
NSArray *arrayB = [set allObjects];
NSLog(#"arrayB is: %#", set);
The output from the above looks like:
arrayA is: (
a,
b,
a,
c,
a
)
arrayB is: {(
b,
c,
a
)}
and to sort a mutable array in ascending order you can use NSSortDescriptor and sortUsingDescriptors:sortDescriptors. Also you need to provide the key on the basis of which array will be sorted.
NSSortDescriptor *sortDescriptor;
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"key" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
[array sortUsingDescriptors:sortDescriptors];
[sortDescriptor release];
Here you will get what you want.
//sort description will used to sort array.
NSSortDescriptor *descriptor=[[NSSortDescriptor alloc] initWithKey:#"iid" ascending:YES];
NSArray *descriptors=[NSArray arrayWithObject: descriptor];
NSArray *reverseOrder=[arrLeft sortedArrayUsingDescriptors:descriptors];
reverseOrder is your desire output.
there is another way you can sort objects that followed model.
NSArray *someArray = /* however you get an array */
NSArray *sortedArray = [someArray sortedArrayUsingComparator:^(id obj1, id obj2) {
NSNumber *rank1 = [obj1 valueForKeyPath:#"iid"];
NSNumber *rank2 = [obj2 valueForKeyPath:#"iid"];
return (NSComparisonResult)[rank1 compare:rank2];
}];
here sortedArray is our output.
you can replace same things for yr key as well.
This is what I did to sort the header data in ascending order and to remove duplicates from both header and leftmost column. Hope this will help others
NSOrderedSet *orderedSet3 = [NSOrderedSet orderedSetWithArray:head3];
headData3 = [[orderedSet3 array] mutableCopy];
[headData3 sortUsingComparator:^NSComparisonResult(NSString *str1, NSString *str2)
{
return [str1 compare:str2 options:(NSNumericSearch)];
}];
NSOrderedSet *orderedSet4 = [NSOrderedSet orderedSetWithArray:left3];
NSMutableArray *arrLeft3 = [[orderedSet4 array] mutableCopy];
[leftTableData3 addObject:arrLeft3];
Here is my requirement :
forSaleSingleProperties array should contain dictionaries with no same attribute PARCELID
forSaleMultipleProperties array should contain an array of those dictionaries with same attribute PARCELID
forSalePropertiesArray is the base array containing all dict.
Note: Dictionaries contain various other attributes. I want all those with same PARCELID attribute
I don't understand what is wrong with this logic...
if (_forSaleSinglePropertiesArray==nil) {
_forSaleSinglePropertiesArray = [[NSMutableArray alloc]initWithObjects: nil];
}
if (_forSaleMultiplePropertiesArray==nil) {
_forSaleMultiplePropertiesArray = [[NSMutableArray alloc]initWithObjects: nil];
}
if (_forSalePropertiesArray!=nil) {
if (_forSalePropertiesArray.count>1) {
BOOL propertyObject1IsMultiple = NO;
NSDictionary *propertyObject1;
NSMutableArray *multiplePinArray = [[NSMutableArray alloc]initWithObjects: nil];
for (int i=0; i<_forSalePropertiesArray.count; i++) {
propertyObject1 = [_forSalePropertiesArray objectAtIndex:i];
multiplePinArray = nil;
multiplePinArray = [[NSMutableArray alloc]initWithObjects: nil];
for (int j=i+1; j<_forSalePropertiesArray.count; j++) {
NSDictionary *propertyObject2 = [_forSalePropertiesArray objectAtIndex:j];
if ([propertyObject1 valueForKey:PARCEL_ID]==[propertyObject2 valueForKey:PARCEL_ID]) {
if (_forSaleMultiplePropertiesArray.count==0) {
[multiplePinArray addObject:propertyObject2];
propertyObject1IsMultiple = YES;
[_forSaleMultiplePropertiesArray addObject:multiplePinArray];
}else{
BOOL propFound = NO;
NSMutableArray *propArr;
NSInteger index = -1;
for(NSMutableArray *arr in _forSaleMultiplePropertiesArray){
if (![arr containsObject:propertyObject2]&&!propFound) {
[arr addObject:propertyObject2];
propertyObject1IsMultiple = YES;
propFound = YES;
index = [_forSaleMultiplePropertiesArray indexOfObject:arr];
propArr = [[NSMutableArray alloc]initWithArray:arr];
}
}
if (propArr!=nil) {
[_forSaleMultiplePropertiesArray replaceObjectAtIndex:index withObject:propArr];
}
}
}
}
if (!propertyObject1IsMultiple) {
[_forSaleSinglePropertiesArray addObject:propertyObject1];
}
}
}
}
OK so...
I'm leaving this as a placeholder.
Sort the parent array by PARCELID.
Iterate array.
Sort into two piles.
... or something. Will write it later.
I would have done it more like this:
- (void)solution {
self.forSaleSinglePropertiesArray = [[NSMutableArray alloc] init];
self.forSaleMultiplePropertiesArray = [[NSMutableArray alloc] init];
NSDictionary *partitionedProperties = partitionPropertiesByParcelID(self.forSalePropertiesArray);
[self dividePropertiesIntoSingleAndMultiple:partitionedProperties];
}
NSDictionary *partitionPropertiesByParcelID(NSArray *properties) {
NSMutableDictionary *result = [[NSMutableDictionary alloc] init];
for (NSDictionary *property in properties) {
id parcelID = property[PARCEL_ID];
NSMutableArray *parcels = result[parcelID];
if (parcels == nil) {
parcels = [[NSMutableArray alloc] init];
[result setObject:parcels forKey:parcelID];
}
[parcels addObject:property];
}
return result;
}
- (void)dividePropertiesIntoSingleAndMultiple:(NSDictionary *)partitionedProperties {
for (NSArray *properties in partitionedProperties.allValues) {
if (properties.count == 1) {
[self.forSaleSinglePropertiesArray addObject:properties[0]];
}
else {
assert(properties.count > 1);
[self.forSaleMultiplePropertiesArray addObject:properties];
}
}
}
First the code creates a Dictionary where the keys are parcel IDs and the values are arrays of properties with that parcle ID. Then it goes through that dictionary and puts a representative of each parcel ID into either the single or multiple array.
I feel that this code is easier to understand, and I strongly suspect that if you do performance metrics on the above code and your answer, that this code will have better performance over large data sets. I believe this is true because your answer seems to have O(n^2) performance while mine is O(n). (Read up on "Big-O notation" if you aren't sure what this means.)
Also, are you sure that your answer actually works for all data sets? Removing objects out of an array while iterating over it throws up a huge red flag in my book.
I liked #Fogmeister's sort idea so much, I implemented it:
- (void)sortSolultion {
self.forSaleSinglePropertiesArray = [[NSMutableArray alloc] init];
self.forSaleMultiplePropertiesArray = [[NSMutableArray alloc] init];
NSArray *forSaleArray = [self.forSalePropertiesArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1[PARCEL_ID] compare:obj2[PARCEL_ID]];
}];
id currentParcelID = nil;
self.propertiesWithCurrentParcelID = [[NSMutableArray alloc] init];
for (NSDictionary *property in forSaleArray) {
if (self.propertiesWithCurrentParcelID.count == 0) {
currentParcelID = property[PARCEL_ID];
}
else if (![property[PARCEL_ID] isEqual: currentParcelID]) {
[self placeCurrentPropertiesInCorrectArray];
currentParcelID = property[PARCEL_ID];
}
[self.propertiesWithCurrentParcelID addObject:property];
}
[self placeCurrentPropertiesInCorrectArray];
}
- (void)placeCurrentPropertiesInCorrectArray {
if (self.propertiesWithCurrentParcelID.count > 1) {
[self.forSaleMultiplePropertiesArray addObject:self.propertiesWithCurrentParcelID];
}
else if (self.propertiesWithCurrentParcelID.count == 1) {
[self.forSaleSinglePropertiesArray addObject:self.propertiesWithCurrentParcelID[0]];
}
[self.propertiesWithCurrentParcelID removeAllObjects];
}
This solution has a slightly higher cyclomatic complexity than my previous solution, and indeed it was harder to get it error free than my first one. But this solution has a smaller allocation footprint. Both seem to have the same big-O complexity.
Alright I got it right myself.
Updated Code to a much simpler and fast mechanism below:
if (_forSaleSinglePropertiesArray==nil) {
_forSaleSinglePropertiesArray = [[NSMutableArray alloc]initWithObjects: nil];
}
if (_forSaleMultiplePropertiesArray==nil) {
_forSaleMultiplePropertiesArray = [[NSMutableArray alloc]initWithObjects: nil];
}
if (_forSalePropertiesArray!=nil) {
NSMutableDictionary *samePropsDict = [[NSMutableDictionary alloc]init];
for (NSDictionary* dict in _forSalePropertiesArray) {
NSMutableArray *samePropsArray = [samePropsDict objectForKey:[dict valueForKey:PARCEL_ID]];
if (samePropsArray==nil) {
samePropsArray = [[NSMutableArray alloc]init];
}
[samePropsArray addObject:dict];
[samePropsDict setObject:samePropsArray forKey:[dict valueForKey:PARCEL_ID]];
}
for (NSString *key in [samePropsDict allKeys]) {
NSArray *arr = [samePropsDict objectForKey:key];
if (arr.count>1) {
[_forSaleMultiplePropertiesArray addObject:arr];
}else{
[_forSaleSinglePropertiesArray addObject:[arr firstObject]];
}
}
}