iphone - 对字典对象数组进行排序
<p><p>我遇到的问题是我无法对 NSMutableDictionary 对象的 NSMutableArray 进行排序,我想按评级对对象进行排序,评级是 NSNumber,我缺少什么?</p>
<p>我当前的代码对“arrayMealRating”中的所有评分求和并对结果数组进行排序:</p>
<pre><code> [arrayMealRating addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:
, @"Airline"
,, @"setMealRating"
, nil]];
}
NSArray *airlineNames = ;
// Loop through all the airlines
for (NSString *airline in airlineNames) {
// Get an array of all the dictionaries for the current airline
NSPredicate *predicate = ;
NSArray *airlineMealRating = ;
// Get the sum of all the ratings using KVC @sum collection operator
NSNumber *rating = ;
//NSLog(@"%@: %@", airline, rating);
[sortedMealArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:
airline, @"Airline"
,, @"setMealRating"
, nil]];
}
NSSortDescriptor *descriptor = [ initWithKey:@"setMealRating"ascending:YES];
];
auxMealRating = ;
</code></pre>
<p>如有任何疑问,请不要投反对票,只要问,我会编辑问题。</p>
<p>最好的问候和对不起我糟糕的英语。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>这应该做你想做的:</p>
<pre><code>NSArray *sortedArray = [arrayMealRating sortedArrayUsingComparator:^(id obj1, id obj2) {
NSNumber *rating1 = [(NSDictionary *)obj1 objectForKey:@"setMealRating"];
NSNumber *rating2 = [(NSDictionary *)obj2 objectForKey:@"setMealRating"];
return ;
}];
</code></pre></p>
<p style="font-size: 20px;">关于iphone - 对字典对象数组进行排序,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/13360709/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/13360709/
</a>
</p>
页:
[1]