ios - 按数字对字典数组进行排序
<p><p>所以我有一个包含大约 180 个字典的数组,每个字典中有 5 个对象(3 个字符串和 2 个 NSNumber)</p>
<p>我正在尝试按其属性之一对每个字典进行排序,这对字符串很有效,但是当涉及到数字时,它无法正确排序。</p>
<pre><code> -(void) sortArrayBy:(int)sortingNumber {
switch (sortingNumber) {
case 0:
NSLog(@"sorting by atomicnumber");
[self.elementsArray sortUsingDescriptors:[NSArray arrayWithObjects:
// When sorting this comes back the same each time but not in the correct order
, nil]];
break;
case 1:
NSLog(@"sorting by name");
, nil]];
break;
case 2:
NSLog(@"sorting by symbol");
, nil]];
break;
case 3:
NSLog(@"sorting by mass");
// When sorting this comes back the same each time but not in the correct order
, nil]];
break;
default:
break;
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>好的,所以我发现了问题,由于某种原因,它是按第一个数字而不是整数排序,所以我使用以下代码:</p>
<pre><code> NSSortDescriptor *sortDescriptor = [NSSortDescriptor
sortDescriptorWithKey:@"ATOMIC MASS" ascending:YES comparator:^(id obj1, id obj2) {
return ;
}];
];
</code></pre></p>
<p style="font-size: 20px;">关于ios - 按数字对字典数组进行排序,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/17920564/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/17920564/
</a>
</p>
页:
[1]