我有一个 NSMutableArray,里面有 30 个字典。每个都包含一个名称和一个值。我目前对名称进行了排序,以便在表格 View 中按字母顺序显示。但是,我想制作一个 UIButton 以提供仍然显示名称的选项,但按值排序。该值不需要显示。另外,这些代码将放在 .m/.h 文件中的什么位置?谢谢!
最佳答案
为了能够帮助您,我需要一份测试数据。假设您的数据数组如下所示:
NSMutableArray *aMutableArray = [NSMutableArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:
@"A", @"name",
[NSNumber numberWithInt:6], @"value", nil],
[NSDictionary dictionaryWithObjectsAndKeys:
@"C", @"name",
[NSNumber numberWithInt:2], @"value", nil],
[NSDictionary dictionaryWithObjectsAndKeys:
@"B", @"name",
[NSNumber numberWithInt:4], @"value", nil], nil];
在这种情况下,这是按值排序的方式:
NSSortDescriptor *aSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"value" ascending:YES];
[aMutableArray sortUsingDescriptors:[NSArray arrayWithObject:aSortDescriptor]];
至于把这段代码放在哪里,把它放在你需要重新排序的地方(当然是在实现 [.m] 文件中)。
HTH,如果有任何问题,请在评论中提问。
关于objective-c - iPhone Obj C——对字典的可变数组进行排序——显示一个字符串但按值排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2149069/