-1

I am trying to set up my TableView, and I am having trouble. The values of my NSArray is different from each other. I know this is hard to understand, but i will try my best to explain. For example

-(void)setUpContacts{
//set up the contacts
NSString *string = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ";

letterArray = [[NSMutableDictionary alloc]init];

Contacts *contact = [[Contacts alloc]init];
contactNumbers = [contact phoneNumbers];

for (NSDictionary* info in contactNumbers) {
    firstLetter = [info objectForKey:@"lastName"];
    index = @"_";
    if([firstLetter length] > 0){
        firstLetter =[firstLetter substringToIndex:1];
        firstLetter= [firstLetter capitalizedString];
        NSRange range = [string rangeOfString:firstLetter];
        if(range.length >= 0){
            index= firstLetter;
        }
    }
    if(![letterArray objectForKey:index]){

        [letterArray setValue:[NSMutableArray array] forKey:index];
    }
    [[letterArray objectForKey:index] addObject:info];    
}NSLog(@"%@",letterArray);}

-(NSArray *)getLetterArraycount{
NSMutableArray *countArray = [[NSMutableArray alloc]init];
NSNumber *subCount;

for(id key_main in letterArray){
    subCount = 0;

    for(id key_sub in [letterArray objectForKey:key_main]){
        subCount = [NSNumber numberWithInt:[subCount intValue]+1];
    }
    [countArray addObject:subCount];

}
return countArray;

}

- (void)viewDidLoad{
[super viewDidLoad];
[self setUpContacts];
countArrays = [self getLetterArraycount];    
indexPaths = [[NSMutableDictionary alloc]init];   

NSArray *temp = [letterArray allKeys];
NSLog(@"%@",temp);

NSLog(@"%@",countArrays);

//set up the tableView
self.myTableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
self.myTableView.delegate = self;
self.myTableView.dataSource = self;
self.title = @"Select Friends";
self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:myTableView];}

and it logs like this in the Console

    C =     (
            {
        firstName = Alex;
        kind = Mobile;
        lastName = Chang;
        name = "Alex Chang (Mobile)";
        number = "(555) 555-5555";
    },
            {
        firstName = YuYu;
        kind = Mobile;
        lastName = Chen;
        name = "YuYu Chen (Mobile)";
        number = "(408) 112-2334";
    },
            {
        firstName = Chris;
        kind = Mobile;
        lastName = Choi;
        name = "Chris Choi (Mobile)";
        number = "(999) 999-9999";
    },
            {
        firstName = Kevin;
        kind = Mobile;
        lastName = Chung;
        name = "Kevin Chung (Mobile)";
        number = "1 (231) 241-2312";
    }
);
H =     (
            {
        firstName = Danny;
        kind = Mobile;
        lastName = Huang;
        name = "Danny Huang (Mobile)";
        number = "(408) 599-9770";
    },
            {
        firstName = Ice;
        kind = Mobile;
        lastName = Huang;
        name = "Ice Huang (Mobile)";
        number = "(408) 444-4444";
    }
);
K =     (
            {
        firstName = Will;
        kind = Mobile;
        lastName = King;
        name = "Will King (Mobile)";
        number = "(415) 123-4567";
    }
);
L =     (
            {
        firstName = "";
        kind = iPhone;
        lastName = LastName;
        name = " LastName (iPhone)";
        number = "(408) 123-2555";
    },
            {
        firstName = david;
        kind = Mobile;
        lastName = lub;
        name = "david lub (Mobile)";
        number = "(666) 666-1111";
    }
);
P =     (
            {
        firstName = Jennifer;
        kind = Mobile;
        lastName = Pie;
        name = "Jennifer Pie (Mobile)";
        number = "(666) 666-6666";
    }
);
S =     (
            {
        firstName = Martin;
        kind = Mobile;
        lastName = Shen;
        name = "Martin Shen (Mobile)";
        number = "(415) 333-1234";
    },
            {
        firstName = Francis;
        kind = Mobile;
        lastName = Shung;
        name = "Francis Shung (Mobile)";
        number = "(408) 345-6789";
    }
);
V =     (
            {
        firstName = Brian;
        kind = Mobile;
        lastName = Vu;
        name = "Brian Vu (Mobile)";
        number = "(408) 234-5678";
    }
);
Y =     (
            {
        firstName = Qiu;
        kind = Mobile;
        lastName = Yang;
        name = "Qiu Yang (Mobile)";
        number = "(408) 567-8901";
    }
);
Z =     (
            {
        firstName = Joy;
        kind = Mobile;
        lastName = Zhou;
        name = "Joy Zhou (Mobile)";
        number = "(408) 765-4321";
    }
);
"_" =     (
            {
        firstName = FirstName;
        kind = Mobile;
        lastName = "";
        name = "FirstName  (Mobile)";
        number = "1 (408) 234-5141";
    }
);

}

2012-04-26 16:14:05.195 UpOut[2693:15803] (
L,
Y,
V,
"_",
Z,
S,
K,
C,
P,
H

MY Question is why doesnt it log "C" first, but instead it Logged "L"

danny huang
  • 155
  • 9
  • 1
    You don't say what that last log is a log of. Is it temp? If so, it's not ordered because the order of keys in a dictionary isn't necessarily (or even usually) the same as the order that you added the keys. Also, it would be a good idea not to confuse yourself by calling a dictionary letterArray. – rdelmar Apr 26 '12 at 23:47

2 Answers2

3

Sounds like you need to learn how to use "NSSortDescriptor" (I've linked the documentation for you).

Here is a related question that might have some useful sample code you can refer to. The key you would want to sort on, in your case, would be the lastName value of each NSDictionary entry.

Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
3

Alphabetical answers consistently get more upvotes:

NSArray *sortedArray = [countArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
Yusuf X
  • 14,513
  • 5
  • 35
  • 47