Re: NSDictionary ordering
Re: NSDictionary ordering
- Subject: Re: NSDictionary ordering
- From: Christopher Holland <email@hidden>
- Date: Sun, 21 Sep 2003 20:10:20 -0500
One thing that you can do is order the keys yourself while using them.
You could do something like this:
1. Load just the keys into a new array.
2. Sort the new array (of keys) however you want to.
3. Now you can look up the values in the order that the keys are stored
in the new array using objectsForKeys method of NSDictionary. The
values will come back in an array that corresponds to the keys array.
You can also give it an object that will be placed in the array if the
key is not found in the dictionary. (see documentation for more
explanation ... I'm not very good at that).
Not perfect, but will do the job, and will be sorted correctly every
time, even if the dictionary is modified.
Example:
allKeysArray = [originalDictionary allKeys];
// Do sorting here...just sort the listKeys array and all should work
out
sortedKeysArray = [allKeysArray
sortedArrayUsingSelector:@selector(compare:)];
sortedColors = [originalDictionary objectsForKeys:sortedKeysArray
notFoundMarker:newObjectThatYouCanDefine];
Christopher Holland
Concept House
email@hidden
<
http://www.concepthouse.com/ >
On Sep 21, 2003, at 12:46 PM, Anders Totland wrote:
>
I know this is a kind of dumb question, but will the allKeys method of
>
an NSDictionary reliably return the same order if the dictionary is
>
unchanged?
>
Is there any kind of logic to their order?
>
Will the dictionary {A = anObject; B = anoherObject; C =
>
yetAnotherObject } return the same allKeys in ACocoaApp at seven in the
>
morning and AnotherCocoaApp at three in the evening?
>
If the one is loaded from a plist and the other is created
>
programaticcally?
>
Is there any kind of ordered dictionary, i.e. where the keys of allKeys
>
are a set, but also ordered?
>
>
>
>
"A guy once asked me for a cup of coffee. So I bit him."
>
_______________________________________________
>
cocoa-dev mailing list | email@hidden
>
Help/Unsubscribe/Archives:
>
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
>
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.