NSDictionary is an unordered collection. You can't keep the original order (or any order) when you store things in a dictionary. For ordered collections, you want NSArray or NSOrderedSet.
NSArray of NSDictionaries into NSDictionary of NSArrays
-
17-06-2023 - |
Pregunta
I have created an NSDictionary of NSArrays based on a keyValue from the NSDictionaries found in the original NSArray. However I have an issue where the values inside the original NSDictionary are all strings but contain numbers.
The reason this is a problem is because my sorted NSMutableArray looks like this
1
2
3
4
5
6
7
8
9
10
11
12
... etc
the issue being is it gets sorted into the NSDictionary of arrays like this
1
10
11
12
2
3
4
5
6
7
8
9
I would like to know how to keep the orginal order found in the original NSMutableArray of NSDictionaries?
This is how I am creating the NSDictionary of Arrays.
NSMutableDictionary *sortedDictionaryArraysForTableView = [NSMutableDictionary dictionary];
for (NSDictionary *dict in xmlMArray) {
NSString *currentNumberString = [dict objectForKey:@"Number"];
if(![[sortedDictionaryArraysForTableView allKeys] containsObject:currentNumberString])
sortedDictionaryArraysForTableView[currentNumberString] = [NSMutableArray array];
[sortedDictionaryArraysForTableView[currentNumberString] addObject:dict];
}
Solución
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow