将数组排序到字典中

发布于 2024-11-28 02:38:45 字数 272 浏览 1 评论 0原文

我有很多字符串的数组。 我不想将它们排序到字典中,因此所有以相同字母开头的字符串都进入一个数组,然后该数组成为键的值;键是其值数组中所有单词开头的字母。

示例

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 技术交流群。

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

发布评论

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

评论(5

季末如歌 2024-12-05 02:38:45
NSArray *list = [NSArray arrayWithObjects:@"apple, animal, bat, ball", nil];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
for (NSString *word in list) {
    NSString *firstLetter = [[word substringToIndex:1] uppercaseString];
    NSMutableArray *letterList = [dict objectForKey:firstLetter];
    if (!letterList) {
        letterList = [NSMutableArray array];
        [dict setObject:letterList forKey:firstLetter];
    }
    [letterList addObject:word];
}
NSLog(@"%@", dict);
NSArray *list = [NSArray arrayWithObjects:@"apple, animal, bat, ball", nil];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
for (NSString *word in list) {
    NSString *firstLetter = [[word substringToIndex:1] uppercaseString];
    NSMutableArray *letterList = [dict objectForKey:firstLetter];
    if (!letterList) {
        letterList = [NSMutableArray array];
        [dict setObject:letterList forKey:firstLetter];
    }
    [letterList addObject:word];
}
NSLog(@"%@", dict);

您可以通过以下步骤实现您想要的:

  1. 创建一个空但可变的字典。
  2. 获取第一个字符。
  3. 如果该角色的键不存在,请创建它。
  4. 将单词添加到键的值中(应该是 NSMutableArray)。
  5. 对所有键重复步骤 #2。

以下是这些步骤的 Objective-C 代码。请注意,我假设您希望按键不区分大小写

// create our dummy dataset
NSArray * wordArray = [NSArray arrayWithObjects:@"Apple", 
                       @"Pickle", @"Monkey", @"Taco", 
                       @"arsenal", @"punch", @"twitch", 
                       @"mushy", nil];
// setup a dictionary
NSMutableDictionary * wordDictionary = [[NSMutableDictionary alloc] init];
for (NSString * word in wordArray) {
    // remove uppercaseString if you wish to keys case sensitive.
    NSString * letter = [[word substringWithRange:NSMakeRange(0, 1)] uppercaseString];
    NSMutableArray * array = [wordDictionary objectForKey:letter];
    if (!array) {
        // the key doesn't exist, so we will create it.
        [wordDictionary setObject:(array = [NSMutableArray array]) forKey:letter];
    }
    [array addObject:word];
}
NSLog(@"Word dictionary: %@", wordDictionary);

You can achieve what you want through the following steps:

  1. Create an empty but mutable dictionary.
  2. Get the first character.
  3. If a key for that character does not exist, create it.
  4. Add the word to the value of the key (should be an NSMutableArray).
  5. Repeat step #2 for all keys.

Here is the Objective-C code for these steps. Note that I am assuming that you want the keys to be case insensitive.

// create our dummy dataset
NSArray * wordArray = [NSArray arrayWithObjects:@"Apple", 
                       @"Pickle", @"Monkey", @"Taco", 
                       @"arsenal", @"punch", @"twitch", 
                       @"mushy", nil];
// setup a dictionary
NSMutableDictionary * wordDictionary = [[NSMutableDictionary alloc] init];
for (NSString * word in wordArray) {
    // remove uppercaseString if you wish to keys case sensitive.
    NSString * letter = [[word substringWithRange:NSMakeRange(0, 1)] uppercaseString];
    NSMutableArray * array = [wordDictionary objectForKey:letter];
    if (!array) {
        // the key doesn't exist, so we will create it.
        [wordDictionary setObject:(array = [NSMutableArray array]) forKey:letter];
    }
    [array addObject:word];
}
NSLog(@"Word dictionary: %@", wordDictionary);
自此以后,行同陌路 2024-12-05 02:38:45

看看这个主题,他们解决了与你几乎相同的问题 -  在 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.

几味少女 2024-12-05 02:38:45

使用它按字母顺序对数组内容进行排序,进一步根据要求进行设计

[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:)];

忘羡 2024-12-05 02:38:45

我刚刚写了这个示例。它看起来很简单,并且可以满足您的需要。

NSArray *names = [NSArray arrayWithObjects:@"Anna", @"Antony", @"Jack", @"John", @"Nikita", @"Mark", @"Matthew", nil];

NSString *alphabet = @"ABCDEFGHIJKLMNOPQRSTUWXYZ";
NSMutableDictionary *sortedNames = [NSMutableDictionary dictionary];

for(int characterIndex = 0; characterIndex < 25; characterIndex++) {
    NSString *alphabetCharacter = [alphabet substringWithRange:NSMakeRange(characterIndex, 1)];
    NSArray *filteredNames = [names filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF BEGINSWITH[C] %@", alphabetCharacter]];        
    [sortedNames setObject:filteredNames forKey:alphabetCharacter];
}

//Just for testing purposes let's take a look into our sorted data
for(NSString *key in sortedNames) {
    for(NSString *value in [sortedNames valueForKey:key]) {
        NSLog(@"%@:%@", key, value);
    }
}

I just wrote this sample. It looks simple and does what you need.

NSArray *names = [NSArray arrayWithObjects:@"Anna", @"Antony", @"Jack", @"John", @"Nikita", @"Mark", @"Matthew", nil];

NSString *alphabet = @"ABCDEFGHIJKLMNOPQRSTUWXYZ";
NSMutableDictionary *sortedNames = [NSMutableDictionary dictionary];

for(int characterIndex = 0; characterIndex < 25; characterIndex++) {
    NSString *alphabetCharacter = [alphabet substringWithRange:NSMakeRange(characterIndex, 1)];
    NSArray *filteredNames = [names filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF BEGINSWITH[C] %@", alphabetCharacter]];        
    [sortedNames setObject:filteredNames forKey:alphabetCharacter];
}

//Just for testing purposes let's take a look into our sorted data
for(NSString *key in sortedNames) {
    for(NSString *value in [sortedNames valueForKey:key]) {
        NSLog(@"%@:%@", key, value);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文