iPhone Obj C-对字典的可变数组进行排序-显示字符串但按值排序 [英] iPhone Obj C -- Sorting a Mutable Array of dictionaries -- display a string but sort by value
问题描述
我有一个带有30个字典的NSMutableArray.每个都包含一个名称和一个值.我目前对名称进行了排序,以便在表视图中按字母顺序显示.但是,我想制作一个UIButton来提供STILL DISPLAYING THE NAMES的选项,但按值排序.该值不需要显示.另外,这些代码将放在.m/.h文件中的什么位置?谢谢!
I have a NSMutableArray with 30 dictionaries inside of it. Each contains a name and a value. I currently have the names sorted so that the show in a table view alphabetically. However, I'd like to make a UIButton to give the option of STILL DISPLAYING THE NAMES, but ordered by the value. The value doesn't need to be displayed. Also, where in the .m/.h files would these codes be placed? THANKS!
推荐答案
为了能够帮助您,我需要使用测试数据.假设您的数据数组如下所示:
To be able to help you I need a test data to work with. Let's say your array with data looks like this:
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];
在这种情况下,这是按值对它进行排序的方式:
In this case case here's how you sort it by value:
NSSortDescriptor *aSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"value" ascending:YES];
[aMutableArray sortUsingDescriptors:[NSArray arrayWithObject:aSortDescriptor]];
关于此代码的放置位置,请将其放置在需要重新排序的位置(当然在实现[.m]文件中).
As for where to place this code place it wherever you need the reordering to happen (in the implementation [.m] file of course).
HTH,如果有任何问题,请在评论中问他们.
HTH and if there will be any questions ask them in the comments.
这篇关于iPhone Obj C-对字典的可变数组进行排序-显示字符串但按值排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!