I'm just learning objective-c and I have an NSMutableDictionary with UITableViewCell sections in it.
The thing is, it's sorted on year and I'd like to reverse that order so the last year comes on top and not bottom.
Any ideas how to reverse sort an NSMutableDictionary?
Thanks in advance
As already stated, an NSDictionary has no order.
What you can do is order the keys in a separate array and then access the keyed objects in the dictionary in the order that the keys appear in your ordered array.
Something like this:
NSArray *orderedKeys = [[dictionary allKeys] sortedArrayUsingDescriptors:<your sort descriptor>];
for (NSString *key in orderedKeys)
{
MyObject *myObject = [dictionary objectForKey:key];
// do what you will with myObject
}
As already pointed out, a dictionary does not guarantee you any order. You might be interested in this related question that shows how to get all dictionary values sorted using a custom function.
An NSDictionary has no implied order.
Related
I am having a problem that I think I am overcomplicating.
I need to make either an NSMutableArray or NSMutableDictionary. I am going to be adding at least two objects like below:
NSMutableArray *results = [[NSMutableArray alloc] init];
[results addObject: [[NSMutableArray alloc] initWithObjects: [NSNumber numberWithInteger:myValue01], #"valueLabel01", nil]];
This gives me the array I need but after all the objects are added I need to be able to sort the array by the first column (the integers - myValues). I know how to sort when there is a key, but I am not sure how to add a key or if there is another way to sort the array.
I may be adding more objects to the array later on.
Quick reference to another great answer for this question:
How to sort NSMutableArray using sortedArrayUsingDescriptors?
NSSortDescriptors can be your best friend in these situations :)
What you have done here is create a list with two elements: [NSNumber numberWithInteger:myValue01] and #"valueLabel01". It seems to me that you wanted to keep records, each with a number and a string? You should first make a class that will contain the number and the string, and then think about sorting.
Doesn't the sortedArrayUsingComparator: method work for you? Something like:
- (NSArray *)sortedArray {
return [results sortedArrayUsingComparator:(NSComparator)^(id obj1, id obj2)
{
NSNumber *number1 = [obj1 objectAtIndex:0];
NSNumber *number2 = [obj2 objectAtIndex:0];
return [number1 compare:number2]; }];
}
I am having an NSMutableDictionary with, being filled dynamically. It looks like this:
{
SomeKey = 6;
AnotherKey = 2;
JustAnotherKey = 28;
}
I need to sort this, so it will be like this one:
{
JustAnotherKey = 28;
SomeKey = 6;
AnotherKey = 2;
}
Is there any way to achieve this? Thanks in advance.
No, sorry!
An NSDictionary doesn't support sorting it's keys - you would have to do that yourself.
Get the keys array from your dictionary, sort that and then go through and get the values from your dictionary. Something like :
NSArray *keys = [myDictionary allKeys];
NSArray *sortedKeys = [keys sortedArrayUsingSelector:#selector(compare:)];
NSArray *values = [NSMutableArray array];
for (id key in sortedKeys)
[values addObject:[myDictionary objectforKey:key]];
Now, values are in the correct order.
However, that's quite a lot of work; if you want them sorted, I would look at storing them in an array to start with?
A Dictionary is an unordered set, which means it doesn't have any order of its elements.So even if you insert the first object as say "one":"first value", and then "two":"second value", when you iterate over the keys, you might get it in any random order(eg: "two' and then "one").
However, if all you want is the values in sorted order, you can iterate over all the keys, fetch the values and store it in an array, and then sort them.
NSArray *values=[myDict allValues];
NSMutableArray *sortedKeys=[[NSMutableArray alloc] init];
NSArray *sortedValues = [values sortedArrayUsingSelector:#selector(yourSelector)];
for (val in sortedArray){
NSString *key=(NSString*)[[myDict allKeysForObject:val] objectAtIndex:0];
[sortedKeys addObject:key]
}
This would be starter for getting first the values in sorted order, and then the corresponding keys. (It is not doing any error checks. So beware of OutOfIndex exceptions).
I am not sure how good would be the efficiency of this code be, coz allKeysForObject would be iterating over all the keys.
Instead of using a NSMutableDictionary, you might consider to use a NSMutableArray and populate it with your own model class which contains a property with the key string and a property with the value.
NSMutableArray provides methods to sort these objects.
Is this helpful enough?
I have an NSMutableArray containing dictionaries of entries. Each entry has several keys of which one is "Title" which contains a name string.
There are many duplicates in the data model because some entries are related to different sections. For example, "Influenza" might be found under "Fever" or "Aches and Pain". So, now I have a key-value pair with two entries called "Influenza" as a title.
When implementing search in a TableViewController, if I type in "Influenza", I get duplicates. In order to remove them, I tried the following:
self.searchEntries = [[NSSet setWithArray:entries]allObjects];
[searchEntries sortedArrayUsingSelector:#selector(compareName:)];
However, this doesn't seem to do anything useful. I still get duplicates and the list isn't in alphabetical order.
Can anyone help me with this seemingly simple matter?
i hope this code help to u
NSSet *uniqueElements = [NSSet setWithArray:myArray];
// iterate over the unique items
for(id element in uniqueElements) {
// do something
}
good luck
Are your duplicates really duplicates? Or are there any special characters/caps?
For sorting, you'll have to do
searchEntries = [searchEntries sortedArrayUsingSelector:#selector(compareName:)];
as - sortedArrayusingSelector: will return an NSArray.
Update:
As entries contains elements of type NSDictionary, you can get the unique & sorted array as follows:
searchEntries = [entries valueForKeyPath:#"#distinctUnionOfObjects.title"];
searchEntries = [searchEntries sortedArrayUsingSelector:#selector(compare:)];
Note that searchEntries now only contains the title values (which might be ok if you just want to display them in a tableView).
First of all the objects in your array all need to respond to the selector you choose. NSDictionary does not respond to compareName: as far as I'm aware.
Secondly, sortedArray... methods create a whole new array. NSMutableArray has some in place sort methods.
Probably the easiest thing is to use if you have a mutable array -sortUsingComparator: e.g.
[[self searchEntries] sortUsingComparator: ^(id obj1, id obj2)
{
return [[obj1 title] compare: [obj2 title]];
}];
If you have an immutable array
sortedArray = [[self searchEntries] sortedArrayUsingComparator: ^(id obj1, id obj2)
{
return [[obj1 title] compare: [obj2 title]];
}];
i have created NSMutableDictionary with 10 keys.Now i want to access NSMutableDictionary keys in a same order as it was added to NSMutableDictionary (using SetValue:* forKey:* );
How can i achieve that ?
If you absolutely must use a dictionary container, you have to use a key that is sortable by the order in which you add key-value pairs. Thus, when creating your dictionary, you use a key that is an auto-incrementing integer or similar. You can then sort on the (integer) keys and retrieve the values associated with those keys.
If you do all of that, however, you may as well just use an NSMutableArray and add values to the array directly! It will be much faster and require less code. You just retrieve objects in order:
for (id obj in myArray) { /* do stuff with obj... */ }
NSMutableDictionary can't do that. Take a look at e.g. Matt Gallaghers OrderedDictionary.
I wrote a quick method to take a source array (of objects that are all out of order) and a reference array (that has objects in a desired (and totally arbitrary) order), and returns an array where the items of the source array have been reorganized to match the reference array.
- (NSArray *) reorderArray:(NSArray *)sourceArray toArray:(NSArray *)referenceArray
{
NSMutableArray *returnArray = [[NSMutableArray alloc] init];
for (int i = 0; i < [referenceArray count]; i++)
{
if ([sourceArray containsObject:[referenceArray objectAtIndex:i]])
{
[returnArray addObject:[arrReference objectAtIndex:i]];
}
}
return [returnArray copy];
}
Note that this is very fragile. It uses NSArray's containsObject: method, which ultimately will call NSObject's isEqual:. Basically, it should work great for arrays of NSStrings, NSNumbers, and maybe NSDates (haven't tried that one yet), but outside of that, YMMV. I imagine if you tried to pass arrays of UITableViewCells or some other really complex object, it would totally sh*t itself, and either crash or return total garbage. Likewise if you were to do something like pass an array of NSDates as the reference array and an array of NSStrings as the source array. Also, if the source array contains items not covered in the reference array, they'll just get discarded. One could address some of these issues by adding a little extra code.
All that said, if you're trying to do something simple, it should work nicely. In your case, you could build up the reference array as you are looping through your setValue:forKey:.
NSMutableArray *referenceArray = [[NSMutableArray alloc] init];
NSMutableDictionary *yourDictionary = [[ NSMutableDictionary alloc] init];
for (//whatever you are looping through here)
{
[yourDictionary setValue://whatever forKey:key];
[referenceArray addObject:key];
}
Then, when you want to loop over your items in the order they came in, you just
for (NSString *key in [self reorderArray:[myDict allKeys] toArray:referenceArray])
Actually you have a reference array in order manner then why you have to add to one more array.So i guess this approach is not good.Please consider my opinion.
Although #GenralMike 's answer works a breeze, it could be optimized by leaving off the unnecessary code as follows:
1) Keep an array to hold reference to the dictionary keys in the order they are added.
NSMutableArray *referenceArray = [[NSMutableArray alloc] init];
NSMutableDictionary *yourDictionary = [[ NSMutableDictionary alloc] init];
for (id object in someArray) {
[yourDictionary setObject:object forKey:someKey];
[referenceArray addObject:someKey]; // add key to reference array
}
2) Now the "referenceArray" holds all of the keys in order, So you can retrieve objects from your dictionary in the same order as they were originally added to the dictionary.
for (NSString *key in referenceArray){
//get object from dictionary in order
id object = [yourDictionary objectForKey:key];
}
I have a NSArray of string id and a NSDictionary of NSDictionary objects. I am currently looping through the string id array to match the id value in the NSDictionary.
There are around 200 NSDictionary objects and only 5 or so string ID.
My current code is such:
for (NSString *Str in aArr) {
for (NSDictionary *a in apArr)
{
if ([a objectForKey:#"id"] == Str)
{
NSLog(#"Found!");
}
}
}
The performance of the above code is really slow and I was wondering if there is a better way to do this?
I'd implement your code in the following way:
for (NSDictionary *a in apArr)
{
if ([aArr containsObject:[a objectForKey:#"id"]])
{
NSLog(#"Found!");
}
}
I'm still not sure about containsObject performance, but, I guess there should be SDK optimizations to find objects faster than O(n).
Added:
Another suggestion. I suppose, that "id" field is unique for all NSDictionary objects. If it is so, you can remap your NSArray of NSDictionaries to NSDictionary:
from:
index -> NSDictionary
to:
id -> NSDictionary
And you will find elements for O(1) instead of O(n).
Re remapping. Either you should create NSDictionary with appropriate format (id -> object) or you may remap your array in the following way:
NSMutableDictionary *md = [[NSMutableDictionary alloc] init];
for ( NSDicationary *a in apArr ) {
[md setObject:a forKey:[a objectForKey:#"id"]];
}
Not sure about the iPhone's cache architecture, but swapping the two loops so that you make as many lookups on the same dictionary before switching to another sounds like it should improve locality. Better locality often leads to (sometimes dramatically) better performance.