How to extract specific data has equal value for some key from NSDictionary into a combined NSArray - iphone

Right now i have a dictionary like this, it's just a example, i got A to Z:
(
{
id = 13;
name = "Roll";
firstLetter = R;
},
{
id = 14;
name = "Scroll";
firstLetter = S;
},
{
id = 16;
name = "Rock";
firstLetter = R;
},
{
id = 17;
name = "Start";
firstLetter = S;
}
)
I want to extract the dict has the same firstLetter and combine these into a NSArray object. The expected results like this:
R array:
(
{
id = 13;
name = "Roll";
firstLetter = R;
},
{
id = 16;
name = "Rock";
firstLetter = R;
}
)
and S array:
(
{
id = 14;
name = "Scroll";
firstLetter = S;
},
{
id = 17;
name = "Start";
firstLetter = S;
}
)
How to do that?

I believe the better method would be the one suggested by Saohooou
But it can be optimised as
NSArray *array = #[#{#"id": #13,#"name":#"Roll",#"firstLetter":#"R"},
#{#"id": #14,#"name":#"Scroll",#"firstLetter":#"S"},
#{#"id": #15,#"name":#"Rock",#"firstLetter":#"R"},
#{#"id": #16,#"name":#"Start",#"firstLetter":#"S"}];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
[array enumerateObjectsUsingBlock:^(NSDictionary *dict, NSUInteger idx, BOOL *stop) {
NSString *key = dict[#"firstLetter"];
NSMutableArray *tempArray = dictionary[key];
if (!tempArray) {
tempArray = [NSMutableArray array];
}
[tempArray addObject:dict];
dictionary[key] = tempArray;
}];
NSLog(#"%#",dictionary);

NSMutableDictionay *dic = [NSMutableDictionay dictionay];
for ( YourObject *obj in yourDic.allValues )
{
NSMutableArray *dateArray = dic[obj.firstLetter];
if ( !dateArray )
{
dateArray = [NSMutableArray array];
[dic setObject:dateArray forKey:obj.firstLetter];
}
[dateArray addObject:obj];
}
so dic is what you want

I assume you organized the dict as an NSArray.
NSMutableDictionary* result = [NSMutableDictionary dictionary]; // NSDictionary of NSArray
for (id entry in dict) {
NSString* firstLetter = [entry firstLetter];
// Find the group of firstLetter
NSMutableArray* group = result[firstLetter];
if (group == nil) {
// No such group --> create new a new one and add it to the result
group = [NSMutableArray array];
result[firstLetter] = group;
}
// Either group has existed, or has been just created
// Add the entry to it
[group addObject: entry];
}
result holds what you want.

try this
NSString *currentStr;
//this int is to detect currentStr
NSInteger i;
NSMutableArray* R_Array = [[NSMutableArray alloc] init];
NSMutableArray* S_Array = [[NSMutableArray alloc] init];
for (NSDictionary *myDict in MyDictArray){
NSString *tempStr = [myDict objectForKey:#"firstLetter"];
if(currentStr = nil && [currentStr isEqualToString:""]){
currentStr = tempStr;
if([currentStr isEqualToString:"R"] ){
[R_Array addObject:myDict];
i = 0;
}else{
[S_Array addObject:myDict];
i = 1;
}
}else{
if([currentStr isEqualToString:tempStr]){
(i=0)?[R_Array addObject:myDict]:[S_Array addObject:myDict];
}else{
(i=0)?[R_Array addObject:myDict]:[S_Array addObject:myDict];
}
}
}
Base on your dictionaries. There are only two type, so i just created two array and use if-else for solving the problem. if there are multy values, you can try switch-case to do it.
Lets do this
NSMutaleDictionary * speDict = [[NSMutableDictionary alloc] init];
for(i=0;i<26;i++){
switch (i){
case 0:
[speDict setObject:[NSMutableArray alloc] init] forKey:#"A"];
break;
case 1:
[speDict setObject:[NSMutableArray alloc] init] forKey:#"B"];
break;
Case 2:
[speDict setObject:[NSMutableArray alloc] init] forKey:#"C"];
break;
...........
Case 25:
[speDict setObject:[NSMutableArray alloc] init] forKey:#"Z"];
break;
}
}
for (NSDictionary *myDict in MyDictArray){
NSString *tempStr = [myDict objectForKey:#"firstLetter"];
switch (tempStr)
case A:
[self addToMySpeDictArrayWithObject:myDict andStr:temStr];
break;
case B:
[self addToMySpeDictArrayWithObject:myDict andStr:temStr];
break;
Case C:
[self addToMySpeDictArrayWithObject:myDict andStr:temStr];
break;
...........
Case Z:
[self addToMySpeDictArrayWithObject:myDict andStr:temStr];
break;
}
-(void)addToMySpeDictArrayWithObject:(NSDictionary*)_dict andStr:(NString*)_str
{
NSMutableArray *tempArray = [speDict objectForKey:_str];
[tempArray addObject:_dict];
}
then the speDict is like
A:
//all firstletter is A
myDict
myDict
myDict
B:
//all firstletter is B
myDict
myDict
.......

First of all the sample you've provided is an array of dicts (not a dict as the question notes). Now, the easiest way to query this array is by using an NSPredicate. Something like this perhaps:
NSArray *objects = ...; // The array with dicts
NSString *letter = #"S"; // The letter we want to pull out
NSPredicate *p = [NSPredicate predicateWithFormat:#"firstLetter == %#", letter];
NSArray *s = [objects filteredArrayUsingPredicate:p]; // All the 'S' dicts
If for some reason you need to group all of your objects without having to ask for a specific letter each time, you could try something like this:
// Grab all available firstLetters
NSSet *letters = [NSSet setWithArray:[objects valueForKey:#"firstLetter"]];
for (NSString *letter in letters)
{
NSPredicate *p = [NSPredicate predicateWithFormat:#"firstLetter == %#", letter];
NSArray *x = [objects filteredArrayUsingPredicate:p];
// Do something with 'x'
// For example append it on a mutable array, or set it as the object
// for the key 'letter' on a mutable dict
}
And of course you could further optimize this approach by implementing a method for filtering the array based on a letter. I hope that this makes sense.

Related

search the values in array in iPhone sdk

I have the array like:
(
{
id=1;
Title="AAAA";
period_id=1;
},
{
id=2;
Title="BBBB";
period_id=2;
},
{
id=3;
Title="CCCC";
period_id=2;
},
{
id=4;
Title="DDDD";
period_id=2;
},
{
id=5;
Title="EEEE";
period_id=3;
},
)
Question: How can i know that Period_id=2 is multiple times in the array?
Help me solve this.
Thank you,
There are lots of ways to do so, Some of them are here ..
A:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"period_id == %#", #"2"];
NSArray *newArray = [array filteredArrayUsingPredicate:predicate];
NSLog(#"%d", [newArray count]);
B:
NSMutableArray *newArray = [[NSMutableArray alloc] init];
for (id obj in array)
{
if([obj[#"period_id"] isEqualToString:#"2"]){
[newArray addObject:obj];
}
}
NSLog(#"%d", [newArray count]);
C:
NSArray *allIds = [array valueForKey:#"period_id"];
NSCountedSet *set = [[NSCountedSet alloc] initWithArray:allIds];
for (id item in set)
{
NSLog(#"period_id=%#, Count=%d", item,[set countForObject:item]);
}
D:
NSArray *allIds = [array valueForKey:#"period_id"];
__block NSMutableArray *newArray = [[NSMutableArray alloc] init];
NSString *valueToCheck = #"2";
[allIds enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if([obj isEqualToString:valueToCheck])
[newArray addObject:obj];
}];
NSLog(#"%d", [newArray count]);
E:
NSIndexSet *indexes = [array indexesOfObjectsPassingTest:^(id obj, NSUInteger idx, BOOL *stop) {
return [[obj objectForKey:#"period_id"] isEqualToString:#"2"];
}];
NSArray *newArray = [array objectsAtIndexes:indexes];
NSLog(#"%d", [newArray count]);
try like this,
NSIndexSet *indices = [questionSections indexesOfObjectsPassingTest:^(id obj, NSUInteger idx, BOOL *stop) {
return [[obj objectForKey:#"period_id"] isEqualToString:#"2"];
}];
NSArray *filtered = [questionSections objectsAtIndexes:indices];
NSLog(#"duplictae:%d\n %#",[indices count],filtered);
O/P:-
duplicate: 3
(
{
name = bbbb;
"period_id" = 2;
},
{
name = ccccc;
"period_id" = 2;
},
{
name = ddddd;
"period_id" = 2;
}
)
if the array is sorted, as it seems at your case, just check if the next item has the same value as this one
for(int i = 0; i < array.size() - 1; i++) {
if (array[i].id == array[i + 1].id) {
// Duplicate
}
}
if you just want to know about id = 2
int idCount = 0;
for(int i = 0; i < array.size() - 1; i++) {
if (array[i].id == 2) {
idCount++;
}
}
if you also want to know the location
int idCount = 0;
int idarr[array.size()];
for(int i = 0; i < array.size() - 1; i++) {
if (array[i].id == 2) {
idarr[idCount++] = i;
}
}
I think this is a JSON response from what I gather. Yes you can get the period_id. Add all the period_id's in an NSMutableArray.
Then simply search for the period_id from within this array for the values of the period_id to be same . You will get the index on which the period_id's are same.
NSSet *uniqueElements = [NSSet setWithArray:myArray];
for(id element in uniqueElements) {
// iterate here
}
You could also use NSPredicate to check duplicate.
Try this Example:
NSPredicate *testPredicate = [NSPredicate predicateWithFormat:#"period_id.intValue == %d",value];
NSMutableArray *data = [[NSMutableArray alloc] init];
NSArray *testArray= [yourArray filteredArrayUsingPredicate:testPredicate];
NSLog(#"duplicate:%d",[testArray count]);

How to assign the NSArray string object values to other NSString variables

NSMutableArray *nameArray = [[NSMutableArray alloc] init];
[nameArray addObject:#"abc"];
[nameArray addObject:#"cdf"];
[nameArray addObject:#"jkl"];
//Use a for each loop to iterate through the array
for (NSString *s in nameArray) {
NSLog(#"value is %#", s);
}
The above code shows all the values of nameArray. But I want to assign all those values to these NSString:
NSString *a;
NSString *b;
NSString *cd;
An aray can have 1 or more elements but not more than 5.
Actually,I have 5 buttons, each button on click will add a NSString value(values are: f1,f2,f3,f4 and f5) to NSMutableArray. Now its upto the user if he clicks 2 buttons or 3 or 5 in a day. Now all these values will be saved in NSMutableArray (which can be 1 or 2 but not more than 5). That NSMutableArray will be saved in NSUserDefaults. This NSMutableArray than will be used in another view where I have some UIImageView (1,2,3,4 and 5). Now when I will get the string values from that Array(f1,f2,f3). If it is f1 then an image will be assigned to UIImage 1 if it is f3 then to image 3 and so on.
How to achieve this?
I would do something like that:
NSArray *array = [NSArray arrayWithObjects:#"A", #"B", #"C", #"D", #"E", nil];
NSString *a = nil, *b = nil, *c = nil, *d = nil, *e = nil;
NSUInteger idx = 0;
for ( NSString *string in array )
{
switch ( idx++ ) {
case 0: a = string; break;
case 1: b = string; break;
case 2: c = string; break;
case 3: d = string; break;
case 4: e = string; break;
}
}
As at least one element will be there in your array:
NSString *a = (NSString *)[nameArray objectAtIndex:0];
As maximum will be five elements:
for(int i = 1;i<[array count];i++)
{
if(i == 1)
{
NSString *b = (NSString *)[nameArray objectAtIndex:1];
}
else if(i == 2)
{
NSString *c = (NSString *)[nameArray objectAtIndex:2];
}
else if(i == 3)
{
NSString *d = (NSString *)[nameArray objectAtIndex:3];
}
else if(i == 4)
{
NSString *e = (NSString *)[nameArray objectAtIndex:4];
}
}
a = [nameArray objectAtIndex:0];
b = [nameArray objectAtIndex:1];
cd = [nameArray objectAtIndex:2];
If you want to put your array elements into separate variables with distinct names, there is no automation in objective c (unlike say, in JavaScript) since it is a compiled and not a interpreted language. Something similar you can achieve with NSDictionary, i.e. to "index" objects with strings or whatever type you want.
You could go on with a simple C array of 5 unsigned chars, where the index of the array would point to your data. Something like this:
unsigned char nameArray[5] = {0, 0, 0, 0, 0};
// if you want to set the 3rd variable, use:
nameArray[2] = 1;
// to query:
if (nameArray[2]) { ... }
// When you need to save it to NSUserDefaults, wrap it into an NSData:
NSData* nameData = [NSData dataWithBytes:nameArray length:sizeof(nameArray)];
[[NSUserDefaults standardUserDefaults] setObject:nameData forKey:#"myKey"];
// To query it:
NSData* nameData = [[NSUserDefaults standardUserDefaults] dataForKey:#"myKey"];
const unsigned char* nameArray2 = [nameData bytes];
unsigned char second = nameArray2[2];
EDITED: corrected array access
If there is at most five options the easiest way to do it is with an if-else-if chain.
NSString *label1 = nil, *label2 = nil, *label3 = nil, *label4 = nil, *label5 = nil;
for (NSString *s in nameArray) {
if (label1 == nil)
label1 = s;
else if (label2 == nil)
label2 = s;
else if (label3 == nil)
label3 = s;
else if (label4 == nil)
label4 = s;
else if (label5 == nil)
label5 = s;
}
NSString *str1=nil;
NSString *str2=nil;
NSString *str3=nil;
NSString *str4=nil;
for (LocationObject *objLoc in arrLocListFirstView)
{
if (objLoc.isLocSelected)
{
for (LocationObject *obj in arrLocListFirstView)
{
if (str1 == nil)
str1 = objLoc.strLoc;
else if (str2 == nil)
str2 = obj.strLoc;
else if (str3 == nil)
str3 = obj.strLoc;
else if (str4 == nil)
str4 = obj.strLoc;
}
NSString *combined = [NSString stringWithFormat:#"%#,%#,%#,%#", str1,str2,str3,str4];
lblLocation.text=combined; (displaying text in UILabel)
}
}

How to sort NSMutableArray elements?

I have model class which contains NSString's- studentName, studentRank and studentImage. I wanna sort the NSMutableArray according to studentRanks. what I have done is
- (void)uploadFinished:(ASIHTTPRequest *)theRequest
{
NSString *response = nil;
response = [formDataRequest responseString];
NSError *jsonError = nil;
SBJsonParser *json = [[SBJsonParser new] autorelease];
NSArray *arrResponse = (NSArray *)[json objectWithString:response error:&jsonError];
if ([jsonError code]==0) {
// get the array of "results" from the feed and cast to NSArray
NSMutableArray *localObjects = [[[NSMutableArray alloc] init] autorelease];
// loop over all the results objects and print their names
int ndx;
for (ndx = 0; ndx < arrResponse.count; ndx++)
{
[localObjects addObject:(NSDictionary *)[arrResponse objectAtIndex:ndx]];
}
for (int x=0; x<[localObjects count]; x++)
{
TopStudents *object = [[[TopStudents alloc] initWithjsonResultDictionary:[localObjects objectAtIndex:x]] autorelease];
[localObjects replaceObjectAtIndex:x withObject:object];
}
topStudentsArray = [[NSMutableArray alloc] initWithArray:localObjects];
}
}
How can I sort this topStudentsArray according to the ranks scored by the Students and If the two or more student have the same rank, How can I group them.
I did like this
TopStudents *object;
NSSortDescriptor * sortByRank = [[[NSSortDescriptor alloc] initWithKey:#"studentRank" ascending:NO] autorelease];
NSArray * descriptors = [NSArray arrayWithObject:sortByRank];
NSArray * sorted = [topStudentsArray sortedArrayUsingDescriptors:descriptors];
but this is not displaying results properly. please help me to overcome this problem. thanks in advance.
doing something like this might do the trick
Initially sort the arrGroupedStudents in the (ascending/descending) order of studentRank
//Create an array to hold groups
NSMutableArray* arrGroupedStudents = [[NSMutableArray alloc] initWithCapacity:[topStudentsArray count]];
for (int i = 0; i < [topStudentsArray count]; i++)
{
//Grab first student
TopStudents* firstStudent = [topStudentsArray objectAtIndex:i];
//Create an array and add first student in this array
NSMutableArray* currentGroupArray = [[[NSMutableArray alloc] initWithCapacity:0] autorelease];
[currentGroupArray addObject:firstStudent];
//create a Flag and set to NO
BOOL flag = NO;
for (int j = i+1; j < [topStudentsArray count]; j++)
{
//Grab next student
TopStudents* nextStudent = [topStudentsArray objectAtIndex:j];
//Compare the ranks
if ([firstStudent.studentRank intValue] == [nextStudent.studentRank intValue])
{
//if they match add this to same group
[currentGroupArray addObject:nextStudent];
}
else {
//we have got our group so stop next iterations
[arrGroupedStudents addObject:currentGroupArray];
// We will assign j-1 to i
i=j-1;
flag = YES;
break;
}
}
//if entire array has students with same rank we need to add it to grouped array in the end
if (!flag) {
[arrGroupedStudents addObject:currentGroupArray];
}
}
Finally your arrGroupedStudents will contain grouped array with equal rank. I have not test run the code so you might need to fix a few things falling out of place. Hope it helps
If you want to display in the order of ranks, you should set the ascending as YES.
NSSortDescriptor * sortByRank = [[NSSortDescriptor alloc] initWithKey:#"studentRank" ascending:YES];
static int mySortFunc(NSDictionary *dico1, NSDictionary *dico2, void *context)
{
NSString *studentName1 = [dico1 objectForKey:#"studentName"];
NSString *studentName2 = [dico2 objectForKey:#"studentName"];
return [studentName1 compare:studentName2];
}
- (IBAction)sortBtnTouched:(id)sender
{
[topStudentsArray sortUsingFunction:mySortFunc context:NULL];
}

Storing in NSMutableDictionaries without knowing how many there will be?

I tried looking for what I need but I did so unsuccessfully.
I am reading in an XML string from a webservice and I need to store my information into core data. I have to have it sorted starting with Department which points to all the Subdepartments in the selected Department and then when a Subdepartment is selected it will list all of the items in that Subdepartment.
The problem with this is I am receiving all my information in Data Tables so it has duplicates of departments and subdepartments.
So to sort it I want to store the information to where I have an array of Dictionaries that hold the name of the departments in each and then an array of Dictionaries that hold the name of the subdepartments in each and then another array of Dictionaries that hold all the item info.
I need it to be like the following...
Array of Department Dictionaries
Department Dictionary
String holding Department Name
Array Of Subdepartment Dictionaries
Subdepartment Dictionary
String holding Subdepartment Name
Array of Item Dictionaries
Item Dictionary
String holding Item Name
String holding more Item Info
But the problem is I don't know how to make this happen so that when I first encounter a Department I save it and then any Subdepartment of that department I encounter later on can just be stored in the array of that Departments dictionary and that any item of a subdepartment would so the same thing. Can I even do this?
(sorry its not pretty I can't figure out how to put it any other way...)
XML Code Example:
<QSR_VIEWS_INVENTORY_ITEMS_LIST diffgr:id="QSR_VIEWS_INVENTORY_ITEMS_LIST1" msdata:rowOrder="0"><CompanyID>104</CompanyID><QSRInventoryItemID>111</QSRInventoryItemID><Description>Test Item 111</Description><Department>_</Department><Subdepartment>_</Subdepartment><SequenceNumber>0</SequenceNumber><CountDisplayUnitName>CA</CountDisplayUnitName><CountDisplayUnitInCase>1.0000</CountDisplayUnitInCase><ReorderAt>0.0000</ReorderAt><ReorderTo>0.0000</ReorderTo><CaseUnitName>CA</CaseUnitName><CaseInCase>1.0000</CaseInCase><PackUnitName>_</PackUnitName><PacksInCase>0.0000</PacksInCase><StackUnitName>_</StackUnitName><StacksInCase>0.0000</StacksInCase><EachUnitName>_</EachUnitName><EachInCase>0.0000</EachInCase><InLocation1>N</InLocation1><InLocation2>N</InLocation2><InLocation3>N</InLocation3><InLocation4>N</InLocation4><InLocation5>N</InLocation5><InLocation6>N</InLocation6><InLocation7>N</InLocation7><InLocation8>N</InLocation8><InLocation9>N</InLocation9><InLocation10>N</InLocation10><InLocation11>N</InLocation11><InLocation12>N</InLocation12><InLocation13>N</InLocation13><InLocation14>N</InLocation14><InLocation15>N</InLocation15><OnShiftCountSheet>Y</OnShiftCountSheet><OnDayCountSheet>Y</OnDayCountSheet><OnWeekCountSheet>Y</OnWeekCountSheet><OnMonthCountSheet>Y</OnMonthCountSheet><OnWasteCountSheet>Y</OnWasteCountSheet><EquivalentToItemID>0</EquivalentToItemID><EquivalentCaseFactor>0.0000</EquivalentCaseFactor></QSR_VIEWS_INVENTORY_ITEMS_LIST>
I think all you need is one extra dictionary which has a key for all of the individual departments and sub departments, then you use that dictionary to check if you already have the new department or not, and if you don't, create it in the appropriate place. The memory overhead should be negligible, the extra dictionary will just have pointers to existing objects.
Answer provided by Alex Mayfield:
NOTE: There are probably way better ways to do this than what I have done.
BOOL addI = YES;
BOOL addS = YES;
BOOL addD = YES;
int I;
int S;
int D;
for(int i = 0; i < [DArray count]; i++)
{
DDictionary = [DArray objectAtIndex:i];
//NSLog([NSString stringWithFormat:#"%# vs %#", soapResults12, [DDictionary objectForKey:#"Title"]]);
if([soapResults12 isEqualToString:[DDictionary objectForKey:#"Title"]])
{
D = i;
//NSLog(#"GOT IT");
SArray = [DDictionary objectForKey:#"Subdepartments"];
for(int k = 0; k < [SArray count]; k++)
{
SDictionary = [SArray objectAtIndex:k];
soapResults11 = [soapResults11 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
//NSLog([NSString stringWithFormat:#"%# vs %#", soapResults11, [SDictionary objectForKey:#"Title"]]);
if([soapResults11 isEqualToString:[SDictionary objectForKey:#"Title"]])
{
S = k;
//NSLog(#"GOT IT");
IArray = [SDictionary objectForKey:#"Items"];
for(int r = 0; r < [IArray count]; r++)
{
IDictionary = [IArray objectAtIndex:r];
//NSLog([NSString stringWithFormat:#"%# vs %#", soapResults1, [IDictionary objectForKey:#"Title"]]);
if([soapResults1 isEqualToString: [IDictionary objectForKey:#"Title"]])
{
//NSLog(#"GOT IT");
I = r;
r = [IArray count];
addI = NO;
addS = NO;
addD = NO;
}
else
{
addI = YES;
addS = NO;
addD = NO;
}
}
k = [SArray count];
}
else
{
addS = YES;
addD = NO;
}
}
i = [DArray count];
}
else
{
addD = YES;
}
}
if(addI && !addS && !addD)
{
IDictionary = [[NSMutableDictionary alloc] init];
[IDictionary setObject:soapResults1 forKey:#"Title"];
[IArray addObject:IDictionary];
[SDictionary setObject:IArray forKey:#"Items"];
[SArray replaceObjectAtIndex:S withObject:SDictionary];
[DDictionary setObject:SArray forKey:#"Subdepartments"];
[DArray replaceObjectAtIndex:D withObject:DDictionary];
}
if(addS && !addD)
{
IDictionary = [[NSMutableDictionary alloc] init];
IArray = [[NSMutableArray alloc] init];
[IDictionary setObject:soapResults1 forKey:#"Title"];
[IArray addObject:IDictionary];
SDictionary = [[NSMutableDictionary alloc] init];
[SDictionary setObject:soapResults11 forKey:#"Title"];
[SDictionary setObject:IArray forKey:#"Items"];
[SArray addObject:SDictionary];
[DDictionary setObject:SArray forKey:#"Subdepartments"];
[DArray replaceObjectAtIndex:D withObject:DDictionary];
}
if(addD)
{
IDictionary = [[NSMutableDictionary alloc] init];
IArray = [[NSMutableArray alloc] init];
[IDictionary setObject:soapResults1 forKey:#"Title"];
[IArray addObject:IDictionary];
SDictionary = [[NSMutableDictionary alloc] init];
SArray = [[NSMutableArray alloc] init];
[SDictionary setObject:soapResults11 forKey:#"Title"];
[SDictionary setObject:IArray forKey:#"Items"];
[SArray addObject:SDictionary];
DDictionary = [[NSMutableDictionary alloc] init];
[DDictionary setObject:soapResults12 forKey:#"Title"];
[DDictionary setObject:SArray forKey:#"Subdepartments"];
[DArray addObject:DDictionary];
}

Converting table view to have sections

I have a table view, which has its data source from an array that contains names of people.
Now to make it easy to find people, I want to section the table view so that it has the letter A-Z on the right hand side, just like the Address Book app.
But my current array just contains a collection of NSStrings. How do I split them so that they are grouped by the first letter of the names? Is there any convenient way to do it?
EDIT: If anyone's interested in my final code:
NSMutableArray *arrayChars = [[NSMutableArray alloc] init];
for (char i = 'A'; i <= 'Z' ; i++) {
NSMutableDictionary *characterDict = [[NSMutableDictionary alloc]init];
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
for (int k = 0; k < [myList count]; k++) {
NSString *currentName = [[friends objectAtIndex:k] objectForKey:#"name"];
char heading = [currentName characterAtIndex:0];
heading = toupper(heading);
if (heading == i) {
[tempArray addObject:[friends objectAtIndex:k]];
}
}
[characterDict setObject:tempArray forKey:#"rowValues"];
[characterDict setObject:[NSString stringWithFormat:#"%c",i] forKey:#"headerTitle"];
[arrayChars addObject:characterDict];
[characterDict release];
[tempArray release];
}
At the end of the function I'll have:
arrayChars [0] = dictionary(headerTitle = 'A', rowValues = {"adam", "alice", etc})
arrayChars[1] = dictionary(headerTitle = 'B', rowValues = {"Bob", etc})
Thank you everyone for your help!
You can use a dictionary to sort them, so create an array with all the letters you want to sort and a array with nil objects to initialize the dictionary
NSArray *names = #[#"javier",#"juan", #"pedro", #"juan", #"diego"];
NSArray *letters = #[#"j", #"p", #"d"];
NSMutableArray *objects = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < [letters count]; ++i)
{
[objects addObject:[[NSMutableArray alloc] init]];
}
NSDictionary *dictionary = [[NSDictionary alloc] initWithObjects:objects forKeys:letters];
Then you must find the first letter if the word and put that word into the corresponding key in the dictionary
for (NSString *name in names) {
NSString *firstLetter = [name substringToIndex:1];
for (NSString *letter in letters) {
if ([firstLetter isEqualToString:letter]) {
NSMutableArray *currentObjects = [dictionary objectForKey:letter];
[currentObjects addObject:name];
}
}
}
To check you can print directly the dictionary
NSLog(#"%#", dictionary);
Then is your work to fill your sections in the tableview using the dictionary