In iOS, the dictionary is actually unordered. How can we sort the dictionary?
The Code is as follows:
@ Interface OrderedDictionary: Optional {NSMutableDictionary * dictionary; NSMutableArray * array;} // insert a location-(void) insertObject :( id) anObject forKey :( id) aKey atIndex :( NSUInteger) anIndex; // obtain the obj-(id) keyAtIndex :( NSUInteger) anIndex; // reverse-(NSEnumerator *) reverseKeyEnumerator; // sequence-(NSEnumerator *) keyEnumerator; @ end
# Import "OrderedDictionary. h "NSString * DescriptionForObject (NSObject * object, id locale, NSUInteger indent) {NSString * objectString; if ([object isKindOfClass: [NSString class]) {objectString = (NSString *) [[object retain] autoreliter];} else if ([object respondsToSelector: @ selector (descriptionWithLocale: indent :)]) {objectString = [(NSDictionary *) object descriptionWithLocale: locale indent: dent: indent];} else If ([object respondsToSelector: @ selector (descriptionWithLocale :)]) {objectString = [(NSSet *) object descriptionWithLocale: locale];} else {objectString = [object description];} return objectString ;}@ implementation OrderedDictionary // initialization method-(id) init {return [self initWithCapacity: 0];} // initialization method-(id) initWithCapacity :( NSUInteger) capacity {self = [super init]; if (self! = Nil) {dictionary = [[NSMutableDictionary alloc] initWithCapacity: capacity]; array = [[NSMutableArray alloc] initWithCapacity: capacity];} return self ;} // destructor-(void) dealloc {[dictionary release]; [array release]; [super dealloc];} // copy method-(id) copy {return [self mutableCopy];} // method of rewriting-(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 *) rev ErseKeyEnumerator {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];} // returns a String object that represents the dictionary content and a list of formatted attributes. -(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
First read a piece of code
NSMutableDictionary *dict2 = [[NSMutableDictionary alloc]initWithCapacity:0]; [dict2 setObject:@"one" forKey:@"1"]; [dict2 setObject:@"two" forKey:@"2"]; [dict2 setObject:@"four" forKey:@"4"]; [dict2 setObject:@"three" forKey:@"3"]; for (NSString *str in [dict2 allKeys]) { NSLog(@"key == %@",str); }
In this result, we can see that the order of the actually stored dictionary is different.
OrderedDictionary *dict = [[OrderedDictionary alloc]initWithCapacity:0]; [dict setObject:@"two" forKey:@"2"]; [dict setObject:@"four" forKey:@"4"]; [dict setObject:@"three" forKey:@"3"]; [dict setObject:@"one" forKey:@"1"]; [dict insertObject:@"five" forKey:@"5" atIndex:0]; NSEnumerator *enumerator2 = [dict keyEnumerator]; id obj; while(obj = [enumerator2 nextObject]) { NSLog(@"%@",obj); }
Source code: dictionary sorting reference article: http://www.cocoawithlove.com/2008/12/ordereddictionary-subclassing-cocoa.html