1

我面临这个问题,当我对字典进行排序时,我无法从中获得输出。在这里,我展示了我的NSMutableDictionary哪个包含。

ReasonMst是我的NSMutableDictionary.

Printing description of ReasonMst:
{
1 =     {
    nReasonNo = 1;
    vReasonCode = 1;
    vReasonDesc = "Office Purpose";
    vReasonType = O;
};
10 =     {
    nReasonNo = 10;
    vReasonCode = 10;
    vReasonDesc = Transit;
    vReasonType = O;
};
2 =     {
    nReasonNo = 2;
    vReasonCode = 2;
    vReasonDesc = Personal;
    vReasonType = O;
};
3 =     {
    nReasonNo = 3;
    vReasonCode = 3;
    vReasonDesc = Specific;
    vReasonType = O;
};
4 =     {
    nReasonNo = 4;
    vReasonCode = 4;
    vReasonDesc = "Meeting";
    vReasonType = I;
};
5 =     {
    nReasonNo = 5;
    vReasonCode = 5;
    vReasonDesc = "Meeting arrange";
    vReasonType = I;
};
6 =     {
    nReasonNo = 6;
    vReasonCode = 6;
    vReasonDesc = "NATIONAL CONFERENCE";
    vReasonType = I;
};
7 =     {
    nReasonNo = 7;
    vReasonCode = 7;
    vReasonDesc = Other;
    vReasonType = O;
};
8 =     {
    nReasonNo = 8;
    vReasonCode = 8;
    vReasonDesc = "Briefing Meeting";
    vReasonType = I;
};
9 =     {
    nReasonNo = 9;
    vReasonCode = 9;
    vReasonDesc = "Meeting At HO";
    vReasonType = I;
};
KeyList =     (
    1,
    2,
    3,
    4,
    5,
    6,
    7,
    8,
    9,
    10,
    ""
);
}

我想明智地将其排序NSMutableDictionaryvReasonDesc。谁能告诉我我该怎么做这个最简单的方法?

4

1 回答 1

1

假设 ReasonMst 是你的 NSMutableDictionary,这样做

NSArray *key=[[NSArray alloc]initWithArray:[ReasonMst objectForKey:@"KeyList"]];
NSMutableArray *vReasonDesc=[[NSMutableArray alloc]init];
NSMutableArray *sortedKeys=[[NSMutableArray alloc]initWithArray:nil];
NSDictionary *tempDict=[[NSDictionary alloc]init];

for (int i=0; i<[key count]; i++) {
    [vReasonDesc addObject:[[ReasonMst objectForKey:[key objectAtIndex:i]]objectForKey:@"vReasonDesc"]];
}
[vReasonDesc sortUsingSelector:@selector(caseInsensitiveCompare:)];    

for (int i=0; i<[vReasonDesc count]; i++) {
    for (int j=0; j<[key count]; j++) {
        tempDict=[ReasonMst objectForKey:[key objectAtIndex:j]];
        NSLog(@"%@ ... %@",[vReasonDesc objectAtIndex:i], tempDict);
        if ([[tempDict objectForKey:@"vReasonDesc"]isEqualToString:[vReasonDesc objectAtIndex:i]]) {
            [sortedKeys addObject:[tempDict objectForKey:@"vReasonNo"]];
        }
    }
}

NSLog(@"sdafa %@",sortedKeys);

sortedKeys 包含根据您的 vReasonDesc 值排序的键......现在您可以根据 sortedKeys 中的键访问您的 ReasonMst。

for (int i=0; i<[sortedKeys count]; i++) {
    NSLog (@"%@",[ReasonMst objectForKey:[sortedKeys objectAtIndex:i]]);
}
于 2012-10-04T06:42:25.913 回答