簡體   English   中英

objective-c 中 Nsdictionary 數組的排序問題

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

我必須使用特定鍵“likecount”按降序對我的數組“數組”進行排序。 我已經寫了下面的代碼,我發現沒有發生任何變化。 這意味着排序沒有執行。 我在下面提到了我的控制台 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 程序

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"是值,數字是鍵。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM