![](/img/trans.png)
[英]Put key Value of NSMutableDictionary of NSMutableArray on UITableViewCell
[英]Sorting NSMutableArray that contains NSMutableDictionary by the dictionary key's value
我有一個帶有70個對象的NSMutableArray
,它們是NSMutableDictionary
,在每個字典中我都有2個值:“ distance”和“ indexPath”。 我想從最小距離到最大距離對NSMutableArray
進行排序。 距離以KM為單位,使用我在此處粘貼的代碼后,數組如下所示:
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"distance" ascending:YES];
[branchDistance sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];
這是此方法的結果:
(lldb) po [branchDistance objectAtIndex:0]
(id) $1 = 0x0d6ac240 {
"<NSIndexPath 0xd6cf180> 1 indexes [0]" = indexPath;
"8.078547" = distance;
}
(lldb) po [branchDistance objectAtIndex:1]
(id) $2 = 0x0d6a64a0 {
"<NSIndexPath 0xd6cf3b0> 1 indexes [1]" = indexPath;
"45.044069" = distance;
}
(lldb) po [bran(lldb) po [branchDistance objectAtIndex:2]
(id) $4 = 0x0d6cf550 {
"<NSIndexPath 0xd69b3f0> 1 indexes [2]" = indexPath;
"9.992081" = distance;
}
(lldb) po [branchDistance objectAtIndex:3]
(id) $5 = 0x0d6cf750 {
"283.356727" = distance;
"<NSIndexPath 0xd6cf480> 1 indexes [3]" = indexPath;
}
我想排序NSMutableArray
從少數到大數目,用這里面的“距離”鍵值NSMutableDictionary
。 我嘗試了一些來自Web和stackoverflow的代碼,但是找不到適合我的東西。
請幫我!
謝謝!
我假設使用NSNumber
對象將距離值存儲在字典中:
NSArray *sortedArray = [branchDistance sortedArrayUsingComparator:^(id obj1, id obj2) {
NSNumber *distance1 = [(NSDictionary *)obj1 objectForKey:@"distance"];
NSNumber *distance2 = [(NSDictionary *)obj2 objectForKey:@"distance"];
return [distance1 compare:distance2];
}];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.