简体   繁体   English

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

[英]Problem in sorting of Nsdictionary array in objective-c

I have to sort my Array "array" with specific key "likecount" with descending order.我必须使用特定键“likecount”按降序对我的数组“数组”进行排序。 I have written below code and I am seeing no any change are being happened.我已经写了下面的代码,我发现没有发生任何变化。 It means sorting is not performing.这意味着排序没有执行。 I mentioned my console output below.我在下面提到了我的控制台 output。

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);

Output of program Output 程序

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

)

You've declared objects and keys in the wrong order.您以错误的顺序声明了对象和键。 It should be,它应该是,

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

That's the reason it's not sorting.这就是它不排序的原因。

You have your keys and your values around the wrong way.你的钥匙和价值观是错误的。 @"likecount" is the value, and the number is the key. @"likecount"是值,数字是键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM