How to sort NSMutableArray elements? - iphone

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];
}

Related

Strange NSRangeException beyond bounds error in NSMutablearray

In my app, I'm doing audio analysis.
Every second, I'm calling some methods, like this one :
- (NSNumber *) arrayAverage: (NSMutableArray *)array
{
int countArray = [array count];
if(!countArray)
return nil;
else if (countArray <= 5 )
return [array valueForKeyPath:#"#avg.doubleValue"];
else
{
// Création et initialisation d'un tableau temporaire
NSMutableArray *averageArray = [[NSMutableArray alloc] initWithArray:array];
NSSortDescriptor* desc = [[NSSortDescriptor alloc] initWithKey:#"self" ascending:YES];
[averageArray sortUsingDescriptors:[NSArray arrayWithObject:desc]];
for (int j = 0; j < countArray / 3; j++)
{
[averageArray removeLastObject];
[averageArray removeObjectAtIndex:0];
}
NSNumber * average = [averageArray valueForKeyPath:#"#avg.doubleValue"];
return average;
}
}
The problem is that I sometime receive a NSRangeException error (after 1min or a few hours...it depends...) saying that the array index is beyond bounds. The strange thing is that the index is NOT out of bounds...
Those methods are only called on the main thread.
Thanks in advance for your help !
EDIT 1 :
With the help of Anim and Abhinav, I have changed my code as following.
It has worked for more than 2hours 45min (which is a record) and then crash with a EXC_BAD_ACCESS code 1 error...
- (NSNumber *) arrayAverage: (NSMutableArray *)array
{
NSArray *arrayCopy = [[NSArray alloc] initWithArray:array]; // CRASH with EXC_BAD_ACCESS error
int countArray = [arrayCopy count];
if(!countArray)
return nil;
else if (countArray <= 5 )
return [arrayCopy valueForKeyPath:#"#avg.doubleValue"];
else
{
// Création et initialisation d'un tableau temporaire
NSMutableArray *averageArray = [[NSMutableArray alloc] initWithArray:arrayCopy];
NSSortDescriptor* desc = [[NSSortDescriptor alloc] initWithKey:#"self" ascending:YES];
[averageArray sortUsingDescriptors:[NSArray arrayWithObject:desc]];
int startOfRange = countArray / 3;
int rangeLength = countArray - 2 * (countArray / 3);
NSArray* slicedArray = [averageArray subarrayWithRange:NSMakeRange(startOfRange, rangeLength)];
NSNumber * average = [slicedArray valueForKeyPath:#"#avg.doubleValue"];
return average;
}
}
With given piece of code it would more of guessing on whats going on here. Please make sure following:
Before calling "removeLastObject" or "removeObjectAtIndex", it makes sense to put a check on the array count. Call them only when there is something to remove.
Given that NSArray and NSMutableArray are not thread safe, make sure that the arrays you are operating on are not getting modified and used at the same time.
I would like to induce following statements inside your For loop:
if (averageArray.count > 0) {
[averageArray removeLastObject];
}
if (averageArray.count > 0) {
[averageArray removeObjectAtIndex:0];
}
you can do one more thing, just add one condition before the loop:
here is the snippet, have added comment:
- (NSNumber *) arrayAverage: (NSMutableArray *)array
{
int countArray = [array count];
if(!countArray)
return nil;
else if (countArray <= 5 )
return [array valueForKeyPath:#"#avg.doubleValue"];
else
{
// Création et initialisation d'un tableau temporaire
NSMutableArray *averageArray = [[NSMutableArray alloc] initWithArray:array];
NSSortDescriptor* desc = [[NSSortDescriptor alloc] initWithKey:#"self" ascending:YES];
[averageArray sortUsingDescriptors:[NSArray arrayWithObject:desc]];
if ([array count] > 0) //Added one condition here---PR Singh
{
for (int j = 0; j < countArray / 3; j++)
{
[averageArray removeLastObject];
[averageArray removeObjectAtIndex:0];
}
}
NSNumber * average = [averageArray valueForKeyPath:#"#avg.doubleValue"];
return average;
}
}

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

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.

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];
}

how to create an C Array

his guys,
i think this is a simple question but i do not know how to do it.
how do i create the line below dynamically from an array?
this is what i need to call.
//data source
NSString * sourceData[7] = {#"2", #"1", #"4", #"8", #"14", #"15", #"10"};
chartData = [WSData dataWithValues:[WSData arrayWithString:sourceData withLen:7]];
+ (NSArray *)arrayWithString:(NSString *[])strings
withLen:(NSUInteger)len {
NSMutableArray *tmpArr = [NSMutableArray
arrayWithCapacity:len];
NSUInteger i;
for (i=0; i<len; i++) {
[tmpArr addObject:strings[i]];
}
return [NSArray arrayWithArray:tmpArr];
}
thanks for all the help especially Daniel :)
this is the answer to the question
NSMutableArray * dayArray = [[NSMutableArray alloc] init];
dayArray = [NSMutableArray arrayWithCapacity:7];
NSMutableArray * sdArray = [[NSMutableArray alloc] init];
sdArray = [NSMutableArray arrayWithCapacity:7];
NSInteger drunked = [appDelegate.drinksOnDayArray count];
if (drunked !=0)
{
for(int i=6; i>=0; i--)
{
DayOfDrinks *drinksOnDay = [appDelegate.drinksOnDayArray objectAtIndex:i];
NSString * dayString= [NSDate stringForDisplayFromDateForChart:drinksOnDay.dateConsumed];
[dayArray addObject:dayString];//X label for graph the day of drink.
drinksOnDay.isDetailViewHydrated = NO;
[drinksOnDay hydrateDetailViewData];
NSNumber *sdNumber = drinksOnDay.standardDrinks;
[sdArray addObject: sdNumber];
}
NSString *sData[7];// = malloc(7 * sizeof(NSString *));
for (int i=0; i<7; i++)
{
DayOfDrinks *drinksOnDay = [appDelegate.drinksOnDayArray objectAtIndex:i];
sData[i] = [NSString stringWithFormat:#"%#",drinksOnDay.standardDrinks];
}
NSString * sourceData[7] = {sData[6],sData[5],sData[4],sData[3],sData[2],sData[1],sData[0] };
}
If you are only using the array as a call parm, and are not storing it somewhere or returning it from your current method:
NSString* sourceData[7];
for (i = 0; i < 7; i++) {
int num = <getTheValueYouWant>;
sourceData[i] = [NSString stringWithFormat:#"%d", num];
}
But note that if you intend to return the array, or store it in some long-lived variable, you need an entirely different setup.
So this is how you would create an NSMutableArray. Mutable because you're creating it at runtime.
NSString * sourceData = [[NSString alloc] initWithFormat:#""];
//assuming the array you have is arr with NSNumber objects
for (NSNumber *num in arr) {
[sourceData stringByAppendingFormat:#"%#", num];
}
You can try below code with the loop as you required.....
NSMutableArray *array;
array = [[NSMutableArray alloc] init];
[array addObject:[NSNumber numberWithFloat:1.0f]];
[array release];

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