iPhone Obj C -- 对可变字典数组进行排序 -- 显示字符串但按值排序

发布于 2024-08-19 08:34:33 字数 156 浏览 6 评论 0原文

我有一个 NSMutableArray,里面有 30 个字典。每个都包含一个名称和一个值。我目前已对名称进行排序,以便按字母顺序显示在表格视图中。但是,我想制作一个 UIButton 来提供仍然显示名称的选项,但按值排序。该值不需要显示。另外,这些代码将放置在 .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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

街角迷惘 2024-08-26 08:34:33

为了能够帮助您,我需要一个测试数据来使用。假设您的数据数组如下所示:

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,如果有任何问题,请在评论中询问。

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]];

As for where to place this code place it wherever you need the reordering to happen (in the implementation [.m] file of course).

HTH and if there will be any questions ask them in the comments.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文