2011-12-10 41 views
0

林很新的iOS和客觀的..我的繼承人的問題.. 如果我的數組是這樣的:排序嵌套的NSDictionary的數組包含數組

myArray = { 
    parentdict = { 
    childdict = { 
     aname = "Aname"; 
     bname - "Bname"; 
     cname = "Cname"; 
    }; 
    childarray = { 
     { 
     counter = "1"; 
     close = "25236"; 
     }, 
     { 
     counter = "2"; 
     close = "12458"; 
     }; 
    }; 
    }; 
}, 
{ 
    parentdict = { 
    childdict = { 
     aname = "Aname"; 
     bname - "Bname"; 
     cname = "Cname"; 
    }; 
    childarray = { 
     { 
     counter = "1"; 
     close = "28556"; 
     }, 
     { 
     counter = "2"; 
     close = "12118"; 
     }; 
    }; 
    }; 
},  
{ 
    parentdict = { 
    childdict = { 
     aname = "Aname"; 
     bname - "Bname"; 
     cname = "Cname"; 
    }; 
    childarray = { 
     { 
     counter = "1"; 
     close = "24356"; 
     }, 
     { 
     counter = "2"; 
     close = "155628"; 
     }; 
    }; 
    }; 
}; 

基本上它嵌套的字典和一個內部的數組的字典包含一個字典(childarray)數組,如果我想通過數組索引1的@「close」對數組myArray進行排序,這就是計數器2旁邊的那個,我應該怎麼做?(也許我應該使用NSSortDescriptor?) 感謝您的回覆

+0

我可能只是使用'sortedArrayUsingFunction:背景:'。 –

回答

0

你已經在JSON格式呈現你的結構。我認爲這是爲了向觀衆介紹。如果你實際上JSON字符串開始,你將不得不將其轉換爲嵌套NSArray IES,並使用一些第三方庫或iOS 5內置類NSDictionary IES ..

假設你已經在你的頂級NSArray* myArray,試試看下面的代碼:

NSArray* myArray = // ... this is your array 
NSArray* sorted_array = [myArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) { 
    NSDictionary* left = obj1; 
    NSDictionary* right = obj2; 
    NSDictionary* left_parent_dict = [left objectForKey: @"parentdict"]; 
    NSDictionary* right_parent_dict = [right objectForKey: @"parentdict"]; 
    NSArray* left_child_array = [left_parent_dict objectForKey: @"childarray"]; 
    NSArray* right_child_array = [right_parent_dict objectForKey: @"childarray"]; 
    NSDictionary* left_child_first = [left_child_array objectAtIndex: 1]; 
    NSDictionary* right_child_first = [right_child_array objectAtIndex: 1]; 
    NSString* left_close = [left_child_first objectForKey: @"close"]; 
    NSString* right_close = [right_child_first objectForKey: @"close"]; 
    NSNumber* left_val = [NSNumber numberWithInt: [left_close intValue]]; 
    NSNumber* right_val = [NSNumber numberWithInt: [right_close intValue]]; 

    return [left_val compare: right_val];   
} ]; 

你當然需要添加一些我爲了簡單而省略的檢查。

如果你想獲得降序修改最後聲明:

return [right_val compare: left_val]; 

我也建議你考慮複雜的數據結構的SQL數據庫由尼科指出。

+0

感謝您的快速答覆... 我提出了基於如何NSLog()呈現數組... ... 'NSDictionary * right_parent_dict = [left objectForKey:@「parentdict」]; 不應該是 'NSDictionary right_parent_dict = [right objectForKey:@「parentdict」]; 這個函數返回一個數組遞增的數據..如果我想要降序我應該怎麼做? 如果我使用NSSortDescriptor很難嗎? – otakuProgrammer

+0

是的,你是對的,這是一個複製粘貼錯誤。我編輯了我的答案,並指出如何獲得降序。當然,你也可以使用NSSortDescriptor。應該不難。 – Krizz

+0

tnx的回覆.. 最後一個問題關於這個kvc的東西..我可以訪問使用 'NSDictionary * thedictionary = [myArray objectAtIndex:index]; '[[字典objectForKey:@「parentdict」] objectForKey:@「childdict」] objectForKey:@「cname」]; //「Cname」我仍然可以使用valueForKeyPath:? like'[thedictionary valueFoKeyPath:@「parentdict.childdict.cname」]; 我今天無法測試代碼,因爲我沒有現在使用的mac .. 因爲在sortdescriptors它使用initWithKey:這是keyPath ..我想..再次感謝:) – otakuProgrammer

0

排序數組可能相當複雜,特別是當數組cont ains嵌套的對象。可能是你應該創建一個SQLite數據庫

0

您應該使用內置JSON API的iOS 5.0。 [這是一個很棒的教程的鏈接。](http://www.raywenderlich.com/5492/working-with-json-in-ios-5)。 JSON非常易於使用。該網站還有關於核心數據的很好的教程(iOS的SQLite API)here.該教程還有兩個部分。

0

試試這個

[array sortUsingComparator:(NSComparator)^(id obj1, id obj2){ 
    int firstValue = [[obj1 objectForKey:@"someKey"] intValue]; 
    int secondValue = [[obj2 objectForKey:@"someKey"] intValue]; 
    int valueDiff = firstValue - secondValue; 
    return (valueDiff == 0) ? NSOrderedSame : (valueDiff < 0) ? NSOrderedAscending : NSOrderedDescending; 
}]; 
+0

它不回答原來的問題。 – Krizz