我正在尝试对“名称”字段上的字典数组进行排序。出了问题,我无法弄清楚。这是我的代码。
myArray = [{'Name' : 'Alina'}{'Name' : 'rita'}{'Name' : 'Viva'}{'Name' : 'baby'}]
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"Name" ascending:YES];
sorted_array = [myArray sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];
sorted_array = [{'Name' : 'Alina'}{'Name' : 'Viva'}{'Name' : 'baby'}{'Name' : 'rita'}
请指导我哪里出错了。谢谢。
最佳答案
您需要使用不区分大小写的比较器:
NSArray *myArray = @[@{@"Name" : @"Alina"}, @{@"Name" : @"rita"}, @{@"Name" : @"Viva"}, @{@"Name" : @"baby"}];
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"Name"
ascending:YES
selector:@selector(localizedCaseInsensitiveCompare:)];
NSArray *sorted_array = [myArray sortedArrayUsingDescriptors:@[descriptor]];
NSLog(@"sorted_array: %@", sorted_array);
NSLog 输出:
sorted_array: (
{
Name = Alina;
},
{
Name = baby;
},
{
Name = rita;
},
{
Name = Viva;
}
)
关于objective-c - 对字典数组进行排序。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24219814/