Sort NSDictionary

I was wondering if anyone could show me how to sort NSDictionary ; I want to read it starting from the last record, since the key is Date + Time, and I want it to be able to be added to NSMutableString . I was able to read it using a counter, but I am not getting the desired results.

thanks

+6
objective-c cocoa nsdictionary
source share
2 answers

For your requirements, the easiest way is to create a new array of keys, sort it, and then use the array to refer to elements from the source dictionary.

(Note that myComparison is your own method that will compare the two keys).

 NSMutableArray* tempArray = [NSMutableArray arrayWithArray:[myDict allKeys]]; [tempArray sortedArrayUsingSelector:@selector(myComparison:)]; for (Foo* f in tempArray) { Value* val = [myDict objectForKey:f]; } 
+13
source share

I have summarized some of the other StackOverflow suggestions for sorting NSDictionary into something very compact that will sort alphabetically. I have a Plist file in the β€œpath” that I load into memory and want to reset.

 NSDictionary *glossary = [NSDictionary dictionaryWithContentsOfFile: path]; NSArray *array1 = [[glossary allKeys] sortedArrayUsingDescriptors:@[ [NSSortDescriptor sortDescriptorWithKey:@"" ascending:YES selector:@selector(localizedStandardCompare:)] ]]; for ( int i=0; i<array1.count; i++) { NSString* key = [array1 objectAtIndex:i]; NSString* value = [glossary objectForKey:key]; NSLog(@"Key=%@, Value=%@", key, value ); } 
0
source share

All Articles