Objective c 对可变词典进行排序

Objective c 对可变词典进行排序,objective-c,Objective C,任何正确方向的指点都将受到感激 我有以下格式的NSD词典: { 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

任何正确方向的指点都将受到感激

我有以下格式的NSD词典:

{
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>";
等等

我有一段代码:

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

}
NSArray*myArray;
myArray=[[valuesDictionary allKeys]使用比较器排序数组:^(id obj1,id obj2){
如果([obj1整型值]>[obj2整型值]){
返回(NSComparisonResult)或撤销;
}
如果([obj1整型值]<[obj2整型值]){
返回(NSComparisonResult)或删除;
}
返回(NSComparisonResult)命令名;
}];
NSMutableDictionary*sortedDisplay=[[NSMutableDictionary alloc]init];
for(myArray中的id对象){
//用物体做某事
[sortedDisplay setObject:[valuesDictionary objectForKey:object]forKey:object];
}
这给了我一个排序的myArray: 1. 2. 3. 4. 5. 6. 7. 8. 9, 10, 11, 12, 13, 14, 15, 十六,

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

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 = "";
{
13=“未给出任何值”

”; 12=“None

”; 11=“一些适应症:遗传性血管性水肿

样本类型为柠檬酸血浆,但也可使用普通血清。

”; 7=“如果无法在一小时内交付,则冻结”

”; 6=“如果样品在一小时内无法到达实验室,则安排在临时现场分离样品,并随后将其运送至实验室。

”; 2=“images/tubes/lightblue\u tube\u large.png”; 1=“功能性C1酯酶抑制剂”; 14 = "|"; 15 = ""; 10=“表示为对照血清的百分比,75%-125%被归类为正常。

”; 9=“储存在-20℃的实验室中”

”; 8=“最多28天”; 5 = ""; 4=“5-10毫升”; 3=“特殊收款程序见附注|”; 16 = "";
}

我做错了什么

我做错了什么

试图对字典进行分类

我做错了什么


正在尝试对字典进行排序。

Tommyo是对的,您无法对
NSDictionary
进行排序。其目的是在性能方面高效地存储和检索数据


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

Tommyo是对的,您无法对
NSDictionary
进行排序。其目的是在性能方面高效地存储和检索数据


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

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

    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

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

    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

可能重复:stackoverflow.com/questions/376090确实探索类似问题可能重复:stackoverflow.com/questions/376090确实探索类似问题Hanks正在尝试为表视图提供一种以可排序格式显示数据的方法。可能是走错路了!谢谢,我们正在尝试为表视图提供一种以可排序格式显示数据的方法。可能是走错路了!谢谢,在结尾使用了NSEnumerator*sectEnum=[myArray objectEnumerator];NSMUTABLEARRY*索引=[[NSMUTABLEARRY alloc]init];id-sKey;虽然((sKey=[sectEnum nextObject]){if([valuesDictionary objectForKey:sKey]!=nil){[index addObject:[valuesDictionary objectForKey:sKey]];}}}谢谢,在结尾使用了nsemulator*sectEnum=[myArray objectEnumerator];NSMUTABLEARRY*索引=[[NSMUTABLEARRY alloc]init];id-sKey;while((sKey=[sectEnum nextObject]){if([valuesDictionary objectForKey:sKey]!=nil){[index addObject:[valuesDictionary objectForKey:sKey]];}