使用 caseInsensitiveCompare 对 NSString 进行排序?
我想要实现的是: 1. 从我的 NSArray 中的所有 NSDictionaries 中获取 NSString(每个 NSDictionary 仅一个 NSString) 2.然后对所有NSString进行排序,使得所有Names按字母顺序排序。
例如: 假设有马克、尼克和罗布。我的数组中有 6 个 NSDictionaries(每个玩家有 2 个 nsdictionaries)。所以它看起来像这样(随机): 马克,罗布,尼克,尼克,罗布,马克
我想这样排序: 马克,马克,尼克,尼克,罗布,罗布
这可能吗?我该怎么做?
谢谢!
What I would like to achieve is:
1. Grab the NSStrings from all the NSDictionaries from my NSArray (Only one NSString per NSDictionary)
2. Then sort all the NSStrings so that all the Names are sorted alphabetically.
For example:
Say there is Mark, Nick, and Rob. There are 6 NSDictionaries in my array (Each player having 2 nsdictionaries). So it would look like this (Randomized):
Mark, Rob, Nick, Nick, Rob, Mark
I want to sort it like this:
Mark, Mark, Nick, Nick, Rob, Rob
Is this possible? How would I do this?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您首先需要的是一个名称数组。假设“dictionaryArray”是字典数组的名称,“name”是每个字典中的键之一:
接下来,对该数组进行排序。在这个特定的示例中,您实际上正在生成它的一个排序的不可变副本:
如果您想要一个可变数组来排序,那么在创建数组“names”之后,您可以创建它的可变版本:
然后您可以对其进行排序到位:
祝您事业顺利。
What you first need is an array of the names. Let's assume that "dictionaryArray" is the name of your array of dictionaries, and that "name" is one of the keys in each of your dictionaries:
Next, you sort this array. In this particular example, you're actually producing a sorted immutable copy of it:
If instead you wanted a mutable array to sort, after creating the array "names," you could make a mutable version of it:
And then you could sort it in place with:
Good luck to you in your endeavors.