1

我正在将密钥加载到 ios 中的选择器控件中。我的问题是我无法按字母顺序排序键。

代码从 UnSorted: Nordfyn, Svendborg, Nyborg, Langeland, Assens, Kerteminde, Middelfart, Odense, Ærø, Faaborg-Midtfyn, 到 Sorted: Ærø, Svendborg, Nyborg, Langeland, Kerteminde, Nordfyn, Middelfart, Assens, Odense , 法博格-米特芬。

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];
4

3 回答 3

3

看起来好像您查看了 kommunerKeys,但字典中键的顺序是未定义的(请参阅链接)。在添加之前对它们进行排序对您没有帮助。

于 2012-07-03T10:33:06.517 回答
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

添加您需要的选项

于 2012-07-03T10:14:59.180 回答
0

你可能正在寻找这个:

 [array sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];

仅仅比较是不够的。

于 2012-07-03T10:15:13.203 回答