开发者

Problem in sorting of Nsdictionary array in objective-c

开发者 https://www.devze.com 2023-03-16 00:44 出处:网络
I have to sort my Array \"array\" with specific key \"likecount\" with descending order. I have written below code and I am seeing no any change are being happened. It means sorting is not performing.

I have to sort my Array "array" with specific key "likecount" with descending order. 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.

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];
NSArra开发者_开发知识库y *sortedarray=[array sortedArrayUsingDescriptors:sortdescriptor];

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

Output of program

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消