1

任何正确方向的指示将不胜感激,

我有一个格式的 NSDictionary:

{
3 = "special-collection-procedures-see-notes|";
13 = "<p>None Given</p>";
16 = "";
6 = "<p>Arrange for sample to be separated at an interim site and subsequently transport frozen to lab, if it cannot reach lab within one hour.</p>";
9 = "<p>Stored in laboratory at -20C</p>";
12 = "<p>None</p>";
2 = "images/tubes/lightblue_tube_large.png";
15 = "";
5 = "";
8 = "Up to 28 days";
11 = "<p>Some indications: Hereditary Angioedema</p><p>Sample type is citrated plasma though plain serum can also be used.</p>";
1 = " Functional C1 esterase inhibitor";
14 = "|";
4 = "5-10 mL";
7 = "<p>Frozen if unable to deliver within one hour</p>";
10 = "<p>expressed as a percentage of a control serum with 75% -125% being classed as normal.</p>";

}

我想对键进行排序,以便顺序为:

1 = " Functional C1 esterase inhibitor";
2 = "images/tubes/lightblue_tube_large.png";

等等。

我有这段代码:

NSArray *myArray; 

myArray = [[valuesDictionary allKeys] sortedArrayUsingComparator: ^(id obj1, id obj2) {

    if ([obj1 integerValue] > [obj2 integerValue]) {

        return (NSComparisonResult)NSOrderedDescending;
    }
    if ([obj1 integerValue] < [obj2 integerValue]) {

        return (NSComparisonResult)NSOrderedAscending;
    }

    return (NSComparisonResult)NSOrderedSame;
}];

NSMutableDictionary *sortedDisplay = [[NSMutableDictionary alloc] init];

for (id object in myArray) {
    // do something with object
    [sortedDisplay setObject:[valuesDictionary objectForKey:object] forKey:object];

}

这给了我一个排序的 myArray:1、2、3、4、5、6、7、8、9、10、11、12、13、14、15、16

但是最终的“排序”可变字典没有排序,即它的

sortedDisplay的打印说明:

{
13 = "<p>None Given</p>";
12 = "<p>None</p>";
11 = "<p>Some indications: Hereditary Angioedema</p><p>Sample type is citrated plasma though plain serum can also be used.</p>";
7 = "<p>Frozen if unable to deliver within one hour</p>";
6 = "<p>Arrange for sample to be separated at an interim site and subsequently transport frozen to lab, if it cannot reach lab within one hour.</p>";
2 = "images/tubes/lightblue_tube_large.png";
1 = " Functional C1 esterase inhibitor";
14 = "|";
15 = "";
10 = "<p>expressed as a percentage of a control serum with 75% -125% being classed as normal.</p>";
9 = "<p>Stored in laboratory at -20C</p>";
8 = "Up to 28 days";
5 = "";
4 = "5-10 mL";
3 = "special-collection-procedures-see-notes|";
16 = "";

}

我究竟做错了什么?

4

3 回答 3

3

我究竟做错了什么?

试图对字典进行排序。

于 2012-07-13T23:10:03.810 回答
3

您将要创建一个排序数组:

    NSArray *sortedKeys = [dictionary keysSortedByValueWithOptions:NSNumericSearch usingComparator:^NSComparisonResult(NSString *a, NSString *b){
        return [a compare:b];
    }];
    //Now you have an array of sorted keys


    NSMutableArray *sortedArray = [[NSMutableArray alloc] initWithCapacity:sortedKeys.count];
    for (NSString *key in sortedKeys){
        NSDictionary *dict = [NSDictionary dictionaryWithObject:[dictionary valueForKey:key] forKey:key];
        [sortedArray addObject:dict];
    }
    //Now you have a sortedArray of key-value pairs
于 2012-07-13T23:26:05.073 回答
2

Tommyo 是对的,你不能对NSDictionary. 其目的是在性能方面有效地存储和检索数据。

如果您需要按特定顺序存储数据,请使用NSArray键(正如您已经在做的那样),并在需要时使用它来访问字典值。

于 2012-07-13T23:16:57.513 回答