如何按 NSArray 内对象内的对象对 NSArray 进行排序?
How do I sort NSArrays by objects inside the objects inside the NSArray?
想想盗梦空间。假设我有 2 个对象 A
和 B
,A
的每个实例都有一个变量 B
,每个 B
都有一个 NSNumber
变量。
我有一个 NSArray Array
,里面有 A
的实例,我想根据 B 中的 NSNumber
变量对它进行排序。所以正常的方法是
NSArray *array = [[NSArray alloc] initWithObjects:a1,a2,a3,a4,a5,nil];
NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"b" ascending: YES];
array = [array sortedArrayUsingDescriptors: [NSArray arrayWithObject:sortOrder]];
只是不会削减它。有什么想法吗?
只要您使用正确的键进行比较,排序就可以使用这种方法正常工作,如下所示:
NSArray *array = [[NSArray alloc] initWithObjects:a1,a2,a3,a4,a5,nil];
NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"b.nsnumbervar" ascending: YES];
array = [array sortedArrayUsingDescriptors: [NSArray arrayWithObject:sortOrder]];
想想盗梦空间。假设我有 2 个对象 A
和 B
,A
的每个实例都有一个变量 B
,每个 B
都有一个 NSNumber
变量。
我有一个 NSArray Array
,里面有 A
的实例,我想根据 B 中的 NSNumber
变量对它进行排序。所以正常的方法是
NSArray *array = [[NSArray alloc] initWithObjects:a1,a2,a3,a4,a5,nil];
NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"b" ascending: YES];
array = [array sortedArrayUsingDescriptors: [NSArray arrayWithObject:sortOrder]];
只是不会削减它。有什么想法吗?
只要您使用正确的键进行比较,排序就可以使用这种方法正常工作,如下所示:
NSArray *array = [[NSArray alloc] initWithObjects:a1,a2,a3,a4,a5,nil];
NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"b.nsnumbervar" ascending: YES];
array = [array sortedArrayUsingDescriptors: [NSArray arrayWithObject:sortOrder]];