ios - 对 NSArray 结果进行排序很奇怪

标签 ios objective-c sorting nsarray

你如何对 NSArray 进行排序,我正在尝试这种方式,但得到的结果很奇怪。

这是我正在使用的代码。

- (void)fetchedData:(NSData *)responseData
{
    NSLog(@"Key to sort = %@",keyToSort);
    NSError* error;
    NSDictionary *mainDic = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
    NSArray *mainArr = [mainDic objectForKey:@"data"];
    // Sortion
    NSSortDescriptor *valueDescriptor = [[NSSortDescriptor alloc] initWithKey:keyToSort ascending:YES];
    NSArray *descriptors = [NSArray arrayWithObject:valueDescriptor];
    NSArray *sortedArray = [mainArr sortedArrayUsingDescriptors:descriptors];

    for (int i = 0; i < [sortedArray count]; i++)
    {
        NSDictionary *subDic = [sortedArray objectAtIndex:i];
        [rankNumber addObject:[subDic valueForKey:keyToSort]]; // rankNumber is an Array
    }
}

输出是

0
1
10
11
12
2
3
4
5
6
7
8
9

任何想法或建议,请帮助解决这个问题

最佳答案

使用以下行:

NSSortDescriptor *valueDescriptor =  [NSSortDescriptor sortDescriptorWithKey:@"statusIdNo" ascending:YES selector:@selector(localizedStandardCompare:)];

代替

NSSortDescriptor *valueDescriptor = [[NSSortDescriptor alloc] initWithKey:keyToSort ascending:YES];

关于ios - 对 NSArray 结果进行排序很奇怪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27958488/

相关文章:

ios - NSTimer 不够精确

ios - Tesseract iOS 相机精度低

ios - UITextView删除UILongPressGestureRecognizer

iphone - UITableViewCell 内的 UIScrollView 在 iPad 上具有水平分页

python - 对 namedtuple 类进行自定义排序

c - 根据另一个数组的内容对 C 数组进行排序

c++ - 冒泡排序、选择排序和插入排序

ios - Swift:每 10 秒重新加载一次 alamofire 请求

iOS - 对象未添加到数组

ios - 如何在 MSMessage 中嵌入 URL?