0

I have 4 NSArray in which each object is dictionary. Something like this:

**Array1 - 30 objects(dictionaries):**

[0]
<Key>Name</key> - <Value>Paul</value>
<Key>City</key> - <Value>New York</value>
<key>Birthday</key> - <value>5.4.1954</value>

[1]
<Key>Name</key> - <Value>John</value>
<Key>City</key> - <Value>New York</value>
<key>Birthday</key> - <value>1.17.1936</value>
etc.

**Array2 - 40 objects(dictionaries):**

[0]
<Key>Name</key> - <Value>Queen</value>
<key>Birthday</key> - <value>5.4.1970</value>

[1]
<Key>Name</key> - <Value>TennesseeKids</value>
<key>Birthday</key> - <value>1.17.1995</value>

etc.

All arrays have the key "birthday".

I Want: combine all the array in one big NSMutableArray/NSMutableDictionary and retrieve first 10 records ordered by date.

I don't ask you how to order my record by date or something, I just want to know what is the best way to implement my idea.

I tried to add all arrays in NSMutableDictionary:

[_mainDictionary setObject:array1 for key:@"firstarray"];
[_mainDictionary setObject:array2 for key:@"secondarray"];
[_mainDictionary setObject:array3 for key:@"thirdarray"];
[_mainDictionary setObject:array4 for key:@"fourarray"];

But I don't think that this is the best way to do it.

Hope for your help. Cheers.

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
Bobby Redjeans
  • 575
  • 1
  • 4
  • 18
  • In order to retrieve the values by date, you need to timestamp them. When you add a new `NSDictionary` object to the array, it should have a `"date"` key with the current date set to be its value. – 67cherries Feb 11 '14 at 14:32
  • Do you really need dictionaries ? I think a better approach is to use simple `Data Objects` and combine those in one array. Then you can filter them with `NSPredicate` – matthisb Feb 11 '14 at 14:39
  • In this big array i want to know the name of the array for each record. For example i need to know that Paul from New York and date 5.9.1954 is from array1 and TennesseeKids is from array2 – Bobby Redjeans Feb 11 '14 at 14:52

2 Answers2

2

You want to combine a number of arrays into a single array. Create an NSMutableArray and call addObjectsFromArray: repeatedly, each time with one of the other arrays as the argument. Now you have one array of dictionaries that you can sort / filter.

Your current code adds the arrays to a dictionary which won't offer you any support with sorting / filtering.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • Sorry, but i forgot to say something. I want to know the name of the array for each record (array1 or array2,3,4)... – Bobby Redjeans Feb 11 '14 at 14:48
  • Are the array contents unique? You could use `contains:` after you have sorted, filtered and selected an item. Or, better, add a key/value pair to the dictionaries to say where they originate from. You may be better off asking about your source data and your requirement from a user point of view. How did you get to multiple different arrays? – Wain Feb 11 '14 at 14:52
  • elf.array1=[NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error]; Do you mean that i need to add extra pair of key-value for each record in array 1? Something like this: nameOfThearray - array1 ? UPDATE: When i try to add extra pair of key-value to array1 it breaks with exception: mutating method sent to immutable object – Bobby Redjeans Feb 11 '14 at 15:09
  • Not need, could (I would). You need to change the JSON options to `NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves` – Wain Feb 11 '14 at 15:10
1

Combine all the arrays into one array.

NSMutableArray *all = [firstArray mutableCopy]
[all addObjectsFromArray:secondArray];
... add 3rd, 4th

Then you can sort that 'all' array. Have a look at How to sort NSArray of objects based on one attribute for an example of sorting the array.

Community
  • 1
  • 1
Graham Perks
  • 23,007
  • 8
  • 61
  • 83