将数组排序到字典中
我有很多字符串的数组。 我不想将它们排序到字典中,因此所有以相同字母开头的字符串都进入一个数组,然后该数组成为键的值;键是其值数组中所有单词开头的字母。
示例
Key = "A" >> Value = "array = apple, animal, alphabet, abc ..."
Key = "B" >> Value = "array = bat, ball, banana ..."
我该怎么做? 预先非常感谢!
I have and array of many strings.
I wan't to sort them into a dictionary, so all strings starting the same letter go into one array and then the array becomes the value for a key; the key would be the letter with which all the words in it's value's array begin.
Example
Key = "A" >> Value = "array = apple, animal, alphabet, abc ..."
Key = "B" >> Value = "array = bat, ball, banana ..."
How can I do that?
Thanks a lot in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以通过以下步骤实现您想要的:
以下是这些步骤的 Objective-C 代码。请注意,我假设您希望按键不区分大小写。
You can achieve what you want through the following steps:
Here is the Objective-C code for these steps. Note that I am assuming that you want the keys to be case insensitive.
看看这个主题,他们解决了与你几乎相同的问题 - 在 Objective-c 中将 NSArray 过滤为新的 NSArray 如果没有帮助,请告诉我,以便我再为您编写一个代码示例。
Take a look at this topic, they solves almost the same problem as you — filtering NSArray into a new NSArray in objective-c Let me know if it does not help so I will write for you one more code sample.
使用它按字母顺序对数组内容进行排序,进一步根据要求进行设计
[keywordListArr sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
Use this to sort the contents of array in alphabetical order, further you design to the requirement
[keywordListArr sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
我刚刚写了这个示例。它看起来很简单,并且可以满足您的需要。
I just wrote this sample. It looks simple and does what you need.