Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective-C:基于字典条目对NSDictionary的键进行排序_Objective C_Xcode_Cocoa Touch - Fatal编程技术网

Objective-C:基于字典条目对NSDictionary的键进行排序

Objective-C:基于字典条目对NSDictionary的键进行排序,objective-c,xcode,cocoa-touch,Objective C,Xcode,Cocoa Touch,好吧,我知道字典是不能分类的。但是,假设我有NSMutableArray*keys=[someDictionary-allKeys]

好吧,我知道字典是不能分类的。但是,假设我有
NSMutableArray*keys=[someDictionary-allKeys]key=someString
,那么我想根据它们对应的字符串对
键进行排序。我认为这是sortUsingComparator的一些应用程序,但在这一点上我有点力不从心

NSDictionary *dict = // however you obtain the dictionary
NSMutableArray *sortedKeys = [NSMutableArray array];

NSArray *objs = [dict allValues];
NSArray *sortedObjs = [objs sortedArrayUsingSelector:@selector(compare:)];
for (NSString *s in sortedObjs)
    [sortedKeys addObjectsFromArray:[dict allKeysForObject:s]];

现在,
sortedKey
将包含按其相应对象排序的键。

只需将其写在此处,因为我在任何地方都找不到它:要根据值从NSDictionary中获取字母数字排序的NSDictionary(在我的情况下,这是必要的),您可以执行以下操作:

NSArray *keys = [someDictionary allKeys];
NSArray *sortedKeys = [keys sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
    NSString *first = [someDictionary objectForKey:a];
    NSString *second = [someDictionary objectForKey:b];
    return [first compare:second];
}];
//sort typeDict alphanumeric to show it in order of values
    NSArray *keys = [typeDict allKeys];

    NSArray *sortedKeys = [keys sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
        NSString *first = [typeDict objectForKey:a];
        NSString *second = [typeDict objectForKey:b];
        return [first compare:second];
    }];
    NSLog(@"sorted Array: %@", sortedKeys);

    NSMutableDictionary *sortedTypeDict = [NSMutableDictionary dictionary];
    int counter = 0;
    for(int i=0; i < [typeDict count]; i++){
        NSString *val = [typeDict objectForKey:[sortedKeys objectAtIndex:counter]];
        NSString *thekey = [sortedKeys objectAtIndex:counter];
        [sortedTypeDict setObject:val forKey:thekey];
        counter++;
    }
    NSLog(@"\n\nsorted dict: %@", sortedTypeDict);
//对typeDict字母数字进行排序以按值顺序显示
NSArray*键=[typeDict allKeys];
NSArray*sortedKeys=[使用比较器排序的键:^N比较结果(id a、id b){
NSString*first=[typeDict objectForKey:a];
NSString*second=[typeDict objectForKey:b];
返回[第一次比较:第二次];
}];
NSLog(@“排序数组:%@”,sortedKeys);
NSMutableDictionary*sortedTypeDict=[NSMutableDictionary];
int计数器=0;
对于(int i=0;i<[typeDict count];i++){
NSString*val=[typeDict objectForKey:[sortedKeys objectAtIndex:counter]];
NSString*thekey=[sortedKeys objectAtIndex:counter];
[sortedTypeDict setObject:val forKey:thekey];
计数器++;
}
NSLog(@“\n\n已选目录:%@”,已选目录);

没什么大不了的

根据字典键对NSDictionary的键进行排序

Abobe用于返回基于字典内容的排序数组,用于返回按字典键排序的数组

NSArray *keys = [theDictionary allKeys];
NSArray *sortedKeys = [keys sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
    return [a compare:b];
}];
NSMutableArray *sortedValues = [NSMutableArray new];
for(NSString *key in sortedKeys)
    [sortedValues addObject:[dictFilterValues objectForKey:key]];

如果字典中的任何值为空,则使用此代码

NSArray *keys = [typeDict allKeys];   
NSSortDescriptor *sd = [[NSSortDescriptor alloc] initWithKey:nil ascending:YES];
NSArray *sortedKeys = [keys sortedArrayUsingDescriptors:@[sd]];
NSLog(@"SORT 1 %@",sortedKeys);

NSMutableDictionary *sortedTypeDict = [NSMutableDictionary dictionary];
int counter = 0;
for(int i=0; i < [typeDict count]; i++){
    id val = [typeDict objectForKey:[sortedKeys objectAtIndex:counter]];
    NSString *thekey = [sortedKeys objectAtIndex:counter];
    [sortedTypeDict setObject:val forKey:thekey];
    counter++;
}
NSLog(@"\n\nsorted dict: %@", sortedTypeDict);
NSArray*keys=[typeDict allKeys];
NSSortDescriptor*sd=[[NSSortDescriptor alloc]initWithKey:nil升序:YES];
NSArray*sortedKeys=[使用描述符进行排序的键:@[sd]];
NSLog(@“分拣1%”,分拣机);
NSMutableDictionary*sortedTypeDict=[NSMutableDictionary];
int计数器=0;
对于(int i=0;i<[typeDict count];i++){
id val=[typeDict objectForKey:[sortedKeys objectAtIndex:counter]];
NSString*thekey=[sortedKeys objectAtIndex:counter];
[sortedTypeDict setObject:val forKey:thekey];
计数器++;
}
NSLog(@“\n\n已选目录:%@”,已选目录);

这里的
分类键
都不是可变字典,它们是不可变的。ops你是说可变数组?我刚刚复制并粘贴了问题haIts中的内容,给出了以下错误:WFMB.V2[21844:f803]-[NSDecimalNumber length]:发送到实例0x6898010的无法识别的选择器。。你知道那是什么吗?当我把它改成objectForKey而不是ValueForkey时,它起作用了。我把这个问题解决了。很抱歉,我没有实际测试它,但很高兴它工作了:)如果它不仅仅是键值呢。如果每个键对应于一个字典,我想按字典中的键“name”排序,该怎么办?对不起,我应该在行动中提到这一点。谢谢你的帮助!它总是“只是”键值,因为这就是NSDictionary的用途。你的意思是非标量值——在这种情况下,你需要将其作为一个函数并应用递归。从性能的角度来看,这不是很聪明,在某些情况下不会返回正确的结果。@Sven和我在OP询问时提到过它——所以我不明白你投反对票的原因。(顺便说一句,如果您担心性能的话,您可以自由地实现这个手动调整的程序集…@H2CO3对于一个多次包含相同值的字典,这也会多次返回这些键。所以像这样的字典{code>{code>a:@“x”,“b:@“x”}
您的代码将返回
[@“a”,“b”,“a”,“b”]
,这在这里肯定是不需要的。因为没有人说永远不会有重复的值,所以你的答案是错误的。顺便说一句,如果你的算法是O(n^2),即使是手动调整的汇编也不会有什么不同。