在objective-c中排序Nsdictionary数组的问题

时间:2011-06-28 06:13:11

标签: iphone objective-c nsarray

我必须使用特定键“likecount”按降序排序我的Array“数组”。我写了下面的代码,我发现没有任何变化。这意味着排序不会执行。我在下面提到了我的控制台输出。

for (NSDictionary *wallText in wallvalue) {
    NSString *wallNameValue = [wallText objectForKey:@"message"];

    if(![[NSNull null] isEqual:wallNameValue] && [wallNameValue length])
    {
        [tableList addObject:wallNameValue];
        NSLog(@" Count Like %@",[likeCount objectAtIndex:i]);

        dict = [NSDictionary dictionaryWithObjectsAndKeys: @"message", wallNameValue, @"likecount", [likeCount objectAtIndex:i], nil];

        [array addObject:dict];

        NSLog(wallNameValue);
    }
    i++;
}

NSSortDescriptor *descriptors=[[[NSSortDescriptor alloc]initWithKey:@"likecount" ascending:YES]autorelease];
NSArray *sortdescriptor=[NSArray arrayWithObject:descriptors];
NSArray *sortedarray=[array sortedArrayUsingDescriptors:sortdescriptor];

NSLog(@"source sorting id key is %@",sortedarray);
NSLog(@"source s@@@@@@  id key is %@",array);

节目输出

source id key is (
    {

    2 = likecount;
    "This wall post is not from application. Direct from website." = message;
},

    {
    0 = likecount;
    "New integration" = message;
},
    {
    1 = likecount;
    "This is beta testing.. yes" = message;
},

    {
    2 = likecount;
    "hello wall testing.." = message;
}

)

2 个答案:

答案 0 :(得分:2)

您已按错误的顺序声明对象和键。它应该是,

dict = [NSDictionary dictionaryWithObjectsAndKeys: wallNameValue, @"message", [likeCount objectAtIndex:i], @"likecount",  nil];

这就是它没有排序的原因。

答案 1 :(得分:0)

你的钥匙和价值观错误。 @"likecount"是值,数字是关键。