简体   繁体   中英

Problem in sorting of Nsdictionary array in objective-c

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];
NSArray *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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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