对 NSArray 结果进行排序很奇怪

Sorting an NSArray result is wiered

你如何对 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];