我遇到问题,当我对字典进行排序时,我无法从中获得输出。
在这里,我展示了包含的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,
""
);
}
我想明智地将此NSMutableDictionary
排序为 vReasonDesc 。任何人都可以告诉我,我怎样才能做到最简单?
答案 0 :(得分: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]]);
}