gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 19:53:20 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com