我正在将密钥加载到ios中的pickercontrol中。 我的问题是我无法按字母顺序排列键。
代码来自UnSorted:Nordfyn,Svendborg,Nyborg,Langeland,Assens,Kerteminde,Middelfart,Odense,Ærø,Faaborg-Midtfyn,分类:Ærø,Svendborg,Nyborg,Langeland,Kerteminde,Nordfyn,Middelfart, Assens,Odense,Faaborg-Midtfyn。
NSBundle *KSbundle = [NSBundle mainBundle];
NSURL *KSplistURL = [KSbundle URLForResource:@"KommunerStedtillæg"
withExtension:@"plist"];
NSDictionary *KSdictionary = [NSDictionary
dictionaryWithContentsOfURL:KSplistURL];
NSArray *KSkeys = [[KSdictionary allKeys] sortedArrayUsingSelector:@selector(compare:)];
NSMutableDictionary *KSnewDict = [NSMutableDictionary dictionary];
for (id key in KSkeys)
[KSnewDict setObject:[KSdictionary objectForKey:key] forKey:key];
kommunerKeys = [KSnewDict allKeys];
kommunerValues = [KSnewDict allValues];
答案 0 :(得分:3)
看起来好像你看kommunerKeys,但是词典中键的顺序是未定义的(参见Link)。在添加之前对它们进行排序会对您有所帮助。
答案 1 :(得分:0)
NSArray *stringsArray = [NSArray arrayWithObjects:
@"string 1",
@"String 21",
@"string 12",
@"String 11",
@"String 02", nil];
static NSStringCompareOptions comparisonOptions = NSCaseInsensitiveSearch | NSNumericSearch |
NSWidthInsensitiveSearch | NSForcedOrderingSearch;
NSLocale *currentLocale = [NSLocale currentLocale];
NSComparator finderSortBlock = ^(id string1, id string2) {
NSRange string1Range = NSMakeRange(0, [string1 length]);
return [string1 compare:string2 options:comparisonOptions range:string1Range locale:currentLocale];
};
NSArray *finderSortArray = [stringsArray sortedArrayUsingComparator:finderSortBlock];
NSLog(@"finderSortArray: %@", finderSortArray);
在此处http://www.developers-life.com/sorting-by-block.html
添加您需要的选项
答案 2 :(得分:0)
你可能正在寻找这个:
[array sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
仅仅比较是不够的。