1
votes

I have an NSMutableDictionary of websites

[dictionaryOfSites setObject:@"http://www.example.com" forKey:@"Example.com"];
[dictionaryOfSites setObject:@"http://www.site1.com" forKey:@"Site1"];
[dictionaryOfSites setObject:@"http://www.apple.com" forKey:@"Apple"];

I know you can't sort a dictionary. But I've read that other people have used an NSMutableArray as the key and the array can be sorted.

So if I setup a new array

[[arrayKey alloc] initWithObjects:@"Example.com", @"Site1", @"Apple", nil];

I would then modify my first snippet to

[dictionaryOfSites setObject:@"http://www.example.com" forKey:[arrayForKey objectAtIndex:0]];
[dictionaryOfSites setObject:@"http://www.site1.com" forKey:[arrayForKey objectAtIndex:1]];
[dictionaryOfSites setObject:@"http://www.apple.com" forKey:[arrayForKey objectAtIndex:2]];

In this simple problem, I had 3 sites so I "hard" coded it. How would I do the same thing if my list of sites was 100? How would the order of the sites be maintained?

If I sort my array [arrayKey sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

Wouldn't index 2 become index 0? If it becomes index 0 then you can see the dictionaryOfSites has the wrong label with the URL.

2
The best way would be to create a custom class which represents your sites, and includes two properties: Name, and URL for instance. Then you create an instance of the class, set the two properties, and put it in an array. Then, you can sort the array based on the name property of the object. - lnafziger
Post your comment as a solution so you can get credit. Can you show me an example of what you're referring to. I am very confused about this - CocoaN00b
Hi CocoaN00b, I normally would but I am headed out the door and won't be back online for a couple of hours. I just wanted to post this as a quick comment to help point you in the right direction. This site is (or should be) more about helping people than "getting credit" anyway. If you still need help when I get back, I will post a more complete example for you, but using the information from my previous comment, you should be able to search for good examples that do just that, both here on SO and on Google. Good luck! - lnafziger
So the main question is this: knowing that your dictionary does not maintain the sort order, can you change it to something else in the beginning, or are you stuck with it and just need a way to copy the data into a sorted array? - lnafziger

2 Answers

1
votes

So you can use a custom class (as I mentioned above in my comment), or better yet use an NSDictionary to store the values as MarkM suggested.

EDIT: "i don't have to maintain a dictionary. its a new app from the ground up."

Since you don't need to start with one big dictionary like you posted, it would be better to just store individual dictionary objects for each site in an array and not have to worry about the conversion.

// Setup the initial array
NSMutableArray *arrayOfSites = [NSMutableArray new];
[arrayOfSites addObject:@{@"Name" : @"Example.com",
                          @"URL"  : @"http://www.example.com"}];
[arrayOfSites addObject:@{@"Name" : @"Site1",
                          @"URL"  : @"http://www.site1.com"}];
[arrayOfSites addObject:@{@"Name" : @"Apple",
                          @"URL"  : @"http://www.apple.com"}];

// At this point, arrayOfSites contains a dictionary object for each site.
// Each dictionary contains two keys:  Name and URL with the appropriate objects.
// Now we just need to sort the array by the Name key in the dictionaries:
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"Name"  ascending:YES];
[arrayOfSites sortUsingDescriptors:[NSArray arrayWithObjects:descriptor, nil]];

NSLog(@"%@", arrayOfSites);

Results:

2013-05-07 18:19:08.386 Testing App[75712:11f03] (
        {
        Name = Apple;
        URL = "http://www.apple.com";
    },
        {
        Name = "Example.com";
        URL = "http://www.example.com";
    },
        {
        Name = Site1;
        URL = "http://www.site1.com";
    } )

To access the data, you would use:

NSString *name = [[arrayOfSites objectAtIndex:indexPath.row] objectForKey:@"Name"];

Note that arrayOfSites should be a declared property of your class so that you can access it from different methods.

0
votes

What you need to do is store your NSDictionary objects in the array and then access a value from that array to do the sorting if you wish. You don't actually store a new string for the sorting. You just check the value of a certain key in the dictionary at the index in the array.

Here is a good source for sorting an array of dictionaries