NSDictionary获取按字母顺序sorting

我正在通过NSDictionary我的函数作为参数。 我希望这是关键和价值观,因为我插入。 例如。 预期产出是:

mutable dict:{ zKey1 = Value1; fKey2 = Value2; aKey3 = Value3; } 

我曾尝试以下方法来创build和设置键的值。

  NSMutableDictionary *mutableDict = [[NSMutableDictionary alloc]init]; [mutableDict setObject:@"Value1" forKey:@"zKey1"]; [mutableDict setObject:@"Value2" forKey:@"fKey2"]; [mutableDict setObject:@"Value3" forKey:@"aKey3"]; NSMutableDictionary *dic2=[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"1004",@"userId",@"cucumber",@"domain",@"168d5c02f ",@"token",@"1004",@"userId1",@"cucumber",@"domain1",@"168d5c02f ",@"token1", nil]; NSDictionary * dict = [NSDictionary dictionaryWithObjects:@[@"Ravi",@"33",@"India",@"India"] forKeys:@[@"name",@"age",@"location",@"country"]]; NSArray *sortedKeys = [[dict allKeys] sortedArrayUsingSelector: @selector(compare:)]; NSMutableArray *sortedValues = [NSMutableArray array]; for (NSString *key in sortedKeys) { [sortedValues addObject: [dict objectForKey: key]]; } NSString *obj1=@"1004"; NSString *obj2=@"cucumber"; NSString *obj3=@"168d5c02f"; NSString *key1=@" userId"; NSString *key2=@"domain"; NSString *key3=@"token"; NSLog(@"dict %@",dict); NSDictionary *dict8 =[NSDictionary dictionaryWithObjects:@[obj1,obj2,obj3] forKeys:@[key1,key2,key3]]; 

但没有任何工作,我总是得到输出

  mutable dict:{ aKey3 = Value3; fKey2 = Value2; zKey1 = Value1; } dict8 { domain = cucumber; token = 168d5c02f; userId = 1004; } dict { age = 33; country = India; location = India; name = Ravi; } dic= { domain = cucumber; domain1 = cucumber; token = "168d5c02f "; token1 = "168d5c02f "; userId = 1004; userId1 = 1004; } 

它总是按照键的字母顺序排列值。 很多人说NSDictionary是一个未分类的容器。 但它得到sorting。 需要帮助绝望。 先谢谢你。

NSDictionary 不是默认sorting的。 它将永远是没有任何秩序。 要创build一个有序的字典,您将需要覆盖现有的数据结构forms。 你可以阅读本教程来达到目的。

总结教程(因为每个人都讨厌只有链接的答案,链接可以随时死掉):

NSDictionary将其密钥存储在一个散列表中,这个散列表是由devise无序的。 由于这种缺乏顺序是散列表存储的基础,你必须执行NSMutableDictionary子类化(并因此重新实现存储)。

在你的.h文件中

 // // OrderedDictionary.h // OrderedDictionary // // Created by Matt Gallagher on 19/12/08. // Copyright 2008 Matt Gallagher. All rights reserved. // // Permission is given to use this source code file without charge in any // project, commercial or otherwise, entirely at your risk, with the condition // that any redistribution (in part or whole) of source code must retain // this copyright and permission notice. Attribution in compiled projects is // appreciated but not required. // #import <Cocoa/Cocoa.h> @interface OrderedDictionary : NSMutableDictionary { NSMutableDictionary *dictionary; NSMutableArray *array; } - (void)insertObject:(id)anObject forKey:(id)aKey atIndex:(NSUInteger)anIndex; - (id)keyAtIndex:(NSUInteger)anIndex; - (NSEnumerator *)reverseKeyEnumerator; @end 

在你的.m文件中:

 // // OrderedDictionary.m // OrderedDictionary // // Created by Matt Gallagher on 19/12/08. // Copyright 2008 Matt Gallagher. All rights reserved. // // Permission is given to use this source code file without charge in any // project, commercial or otherwise, entirely at your risk, with the condition // that any redistribution (in part or whole) of source code must retain // this copyright and permission notice. Attribution in compiled projects is // appreciated but not required. // #import "OrderedDictionary.h" NSString *DescriptionForObject(NSObject *object, id locale, NSUInteger indent) { NSString *objectString; if ([object isKindOfClass:[NSString class]]) { objectString = (NSString *)[[object retain] autorelease]; } else if ([object respondsToSelector:@selector(descriptionWithLocale:indent:)]) { objectString = [(NSDictionary *)object descriptionWithLocale:locale indent:indent]; } else if ([object respondsToSelector:@selector(descriptionWithLocale:)]) { objectString = [(NSSet *)object descriptionWithLocale:locale]; } else { objectString = [object description]; } return objectString; } @implementation OrderedDictionary - (id)init { return [self initWithCapacity:0]; } - (id)initWithCapacity:(NSUInteger)capacity { self = [super init]; if (self != nil) { dictionary = [[NSMutableDictionary alloc] initWithCapacity:capacity]; array = [[NSMutableArray alloc] initWithCapacity:capacity]; } return self; } - (void)dealloc { //This method is pre-ARC. Manual Release commands don't work now. //[dictionary release]; //[array release]; //[super dealloc]; } - (id)copy { return [self mutableCopy]; } - (void)setObject:(id)anObject forKey:(id)aKey { if (![dictionary objectForKey:aKey]) { [array addObject:aKey]; } [dictionary setObject:anObject forKey:aKey]; } - (void)removeObjectForKey:(id)aKey { [dictionary removeObjectForKey:aKey]; [array removeObject:aKey]; } - (NSUInteger)count { return [dictionary count]; } - (id)objectForKey:(id)aKey { return [dictionary objectForKey:aKey]; } - (NSEnumerator *)keyEnumerator { return [array objectEnumerator]; } - (NSEnumerator *)reverseKeyEnumerator { return [array reverseObjectEnumerator]; } - (void)insertObject:(id)anObject forKey:(id)aKey atIndex:(NSUInteger)anIndex { if (![dictionary objectForKey:aKey]) { [self removeObjectForKey:aKey]; } [array insertObject:aKey atIndex:anIndex]; [dictionary setObject:anObject forKey:aKey]; } - (id)keyAtIndex:(NSUInteger)anIndex { return [array objectAtIndex:anIndex]; } - (NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level { NSMutableString *indentString = [NSMutableString string]; NSUInteger i, count = level; for (i = 0; i < count; i++) { [indentString appendFormat:@" "]; } NSMutableString *description = [NSMutableString string]; [description appendFormat:@"%@{\n", indentString]; for (NSObject *key in self) { [description appendFormat:@"%@ %@ = %@;\n", indentString, DescriptionForObject(key, locale, level), DescriptionForObject([self objectForKey:key], locale, level)]; } [description appendFormat:@"%@}\n", indentString]; return description; } @end 

你可以在这里下载Matt Gallagher的 orderedDictionary。