我正在尝试对'名称'上的字典数组进行排序。领域。出了点问题,我无法弄明白。这是我的代码。
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'}
指导我在哪里出错。感谢。
答案 0 :(得分:2)
您需要使用不区分大小写的比较器:
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;
}
)