我必须用特定的键"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;
}
)
您以错误的顺序声明了对象和键。应该是
dict = [NSDictionary dictionaryWithObjectsAndKeys: wallNameValue, @"message", [likeCount objectAtIndex:i], @"likecount", nil];
这就是它没有排序的原因。
您的键和值使用了错误的方法。@"likecount"
是值,数字是键。