我正在尝试将一组NSDictionaries排序为基于NSDictionary valueForKey:
首字母之一的alphabatised数组数组
有效地我想要一些看起来像这样的东西
Array A // contains NSDictionaies with keyvalue MAF strings starting with A
NSDictionary1
NSDictionary2
NSDictionary3
Array B // contains NSDictionaies with keyvalue MAF strings starting with B
NSDictionary1
NSDictionary2
NSDictionary3
Array C // contains NSDictionaies with keyvalue MAF strings starting with C
NSDictionary1
NSDictionary2
NSDictionary3
etc
所以我有一个Letters数组,在每个字母数组中,我希望有一个NSVictionary值的数组,keyValue MAF的首字母应该匹配。
到目前为止,我有一系列字典,我已按此类别排序
NSSortDescriptor *sortDescriptor;
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"MAF"
ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
sortedDictionaryArray = [arrayData sortedArrayUsingDescriptors:sortDescriptors]; // use this in cellforrow: and didselectrow:
NSLog(@"%@", sortedDictionaryArray); // this gives me a sorted array of NSdictionaries based off their MAF keyvalue.
这就是NSDicitonary的样子
HADM = 1;
ISLOT = 0;
ISVERT = 0;
MAF = "Yazot"; // keyvalue I am trying to work with.
然后我卡住了,我不知道从哪里开始。如果有人能帮我理解如何创建一个NSArrays NSArray NSArray那将是伟大的!希望我已经解释了我正在尝试做的事情,如果你需要更多的细节,请告诉我,我会提供。
答案 0 :(得分:2)
此代码将首先使用分配给键@"MAF"
的对象对字典数组进行排序。它将创建一个新的Dictionary,它将为该字符串的每个第一个字母添加一个可变数组,并将该对象添加到它。
NSArray *array = @[@{@"MAF": @"Yazot"}, @{@"MAF": @"Lorem"},@{@"MAF": @"Ipsum"}, @{@"MAF": @"Laura"},@{@"MAF": @"Isit"}];
NSArray *sortedArray = [array sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *obj1, NSDictionary *obj2) {
return [obj1[@"MAF"] compare:obj2[@"MAF"]];
}];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
for (NSDictionary *dict in sortedArray) {
NSString *firstLetter = [dict[@"MAF"] substringToIndex:1];
if(![[dictionary allKeys] containsObject:firstLetter])
dictionary[firstLetter] = [NSMutableArray array];
[dictionary[firstLetter] addObject:dict];
}
如果你需要不区分大小写的字符串处理,“L”和“l”应该放在同一个数组中,做
NSArray *array = @[@{@"MAF": @"Yazot"}, @{@"MAF": @"Lorem"},@{@"MAF": @"Ipsum"}, @{@"MAF": @"Laura"},@{@"MAF": @"Isit"}];
NSArray *sortedArray = [array sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *obj1, NSDictionary *obj2) {
return [obj1[@"MAF"] caseInsensitiveCompare:obj2[@"MAF"]];
}];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
for (NSDictionary *dict in sortedArray) {
NSString *firstLetter = [dict[@"MAF"] substringToIndex:1];
firstLetter = [firstLetter lowercaseString];
if(![[dictionary allKeys] containsObject:firstLetter])
dictionary[firstLetter] = [NSMutableArray array];
[dictionary[firstLetter] addObject:dict];
}
字典结果
{
i = (
{
MAF = Ipsum;
},
{
MAF = Isit;
}
);
l = (
{
MAF = Laura;
},
{
MAF = Lorem;
}
);
y = (
{
MAF = Yazot;
}
);
}
答案 1 :(得分:1)
我们可以从创建字典数组的字典开始。
NSMutableDictionary *dictsForFirstLetter = [[NSMutableDictionary alloc] init];
for (NSDictionary *dict in arrayData) {
NSString *dictMAF = dict[@"MAF"];
NSString *firstLetter = [[dictMAF substringToIndex:1] uppercaseString];
NSMutableArray *dicts = dictsForFirstLetter[firstLetter];
if (!dicts) {
dicts = [[NSMutableArray alloc] init];
dictsForFirstLetter[firstLetter] = dicts;
}
[dicts addObject:dict];
}
然后你可以通过MAF值的第一个字母检索字典数组:
NSArray *dicts = dictsForFirstLetter[@"Y"];
要从中获取字母数组,您可以对字典的键进行排序并对其进行枚举:
NSMutableArray *alphabetizedDicts = [[NSMutableArray alloc] init];
NSArray *firstLetters = [dictsForFirstLetter allKeys];
NSArray *sortedFirstLetters = [firstLetters sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
for (NSString *firstLetter in sortedFirstLetters) {
NSArray *dicts = dictsForFirstLetter[firstLetter];
[alphabetizedDicts addObject:dicts];
}