Hello All
I have a Dictionary object containing key value objects. I want to be able to retrieve the key-values by sort order on the key field. Any ideas or pointers would be greatly appreciated. At the moment everything is being returned unsorted.
The key field is a year date and the value a URL to that year's archive.
Thanks in advance for the information.
"r00t" == r00t uk r00tuk@gmail.com writes:
r00t> I have a Dictionary object containing key value objects. I want to be r00t> able to retrieve the key-values by sort order on the key field. Any r00t> ideas or pointers would be greatly appreciated. At the moment r00t> everything is being returned unsorted.
r00t> The key field is a year date and the value a URL to that year's archive.
This might be a bit perverse, but if you flatten the dictionary into an OrderedCollection of Associations, you can sort those:
yourDictionary associations asSortedCollection do: [:each | year := each key. url := each value. ... ]
Browse the protocol of Association to know why #< does the right thing for a sort.
There might be a far better way.. but I found this in a few minutes.
On 25.08.2009, at 23:31, Randal L. Schwartz wrote:
There might be a far better way.. but I found this in a few minutes.
Someone should make a Squeak version of
- Bert -
Thanks Randal! Your answer did the trick.
Bert - I don't think creating a Squeak version of that would have helped anybody new to squeak/smalltalk. Cookbook wiki maybe?
2009/8/25 Bert Freudenberg bert@freudenbergs.de
On 25.08.2009, at 23:31, Randal L. Schwartz wrote:
There might be a far better way.. but I found this in a few minutes.
Someone should make a Squeak version of
http://xkcd.com/627/
- Bert -
Beginners mailing list Beginners@lists.squeakfoundation.org http://lists.squeakfoundation.org/mailman/listinfo/beginners
On 25.08.2009, at 23:52, r00t uk wrote:
Bert - I don't think creating a Squeak version of that would have helped anybody new to squeak/smalltalk.
Right, in this case not, but that's not what I meant primarily. Should have changed the subject and moved to squeak-dev, sorry. I'll make up for it, see below [*].
I meant that I've never seen a graphical explanation of what Randal did in these "few minutes" to find out. It's what experienced Smalltalkers do all the time, and Squeak provides powerful ways of finding out about itself, but they are not obvious to newbies.
Usually we don't even mention that it took us a minute to find out, so beginners get the impression that it's "all in our heads". It's not (not *all* anyway) but the tools allow us to find out quickly. Faster than looking up documentation indeed, which we can't really proof because of the lack of it ;)
Cookbook wiki maybe?
2009/8/25 Bert Freudenberg bert@freudenbergs.de
On 25.08.2009, at 23:31, Randal L. Schwartz wrote:
There might be a far better way.. but I found this in a few minutes.
Someone should make a Squeak version of
http://xkcd.com/627/
- Bert -
[*] Here's what I'd do:
Make a little test case in a workspace:
| dict | dict := {'a' -> 'first'. 'b' -> 'second'. 'c' ->'third'} as: Dictionary. dict
This intentionally does not use integers as keys, because that's what's special about Dictionaries. (and in fact I didn't really open a workspace but typed it right in the browser I happened to have open. Saves a second)
When I printed the expression above, the elements *were* sorted by key. So this may be chance, but I wanted to see. So I double-clicked Dictionary, pressed cmd-b to bring up a browser, clicked in the right pane with all the methods, and pressed p, to find printing-related methods (I should have clicked the "printing" protocol but usually methods are named more sanely than they are categorized). Anyway this took me right to the "printElementsOn: aStream" which indeed sorts. That could be a template for your own solution.
But in any case the print string of the dictionary is not helpful to show the problem. So:
| dict | dict := {'a' -> 'first'. 'b' -> 'second'. 'c' ->'third'} as: Dictionary. dict asArray
Yes, this printed #('second' 'third' 'first'), so now we need to sort. I have the idiom in my head but I typed in in anyway to verify
| dict | dict := {'a' -> 'first'. 'b' -> 'second'. 'c' ->'third'} as: Dictionary. dict keys asSortedCollection collect: [:key | dict at: key]
And this indeed gives an OrderedCollection('first' 'second' 'third')
So if I had sent an answer to your question I probably would have just pasted the line
dict keys asSortedCollection collect: [:key | dict at: key]
not even mentioning that it took me a minute to verify, and that en passant I discovered that dictionary printing is sorted for a couple of years already, which I had not noticed ;)
Comparing this to Randal's suggestion I'd say that mine feels more "Smalltalky". Accessing the associations in a dictionary directly feels unclean. Maybe because it breaks the abstraction, associations are an implementation detail of dictionaries. It's unlikely this will ever change and accessing the associations is fine, but still ;)
- Bert -
Thanks Bert
That explanation was really useful and the length to which you went in describing the "solution" process you went through is appreciated.
Regards
2009/8/26 Bert Freudenberg bert@freudenbergs.de
On 25.08.2009, at 23:52, r00t uk wrote:
Bert - I don't think creating a Squeak version of that would have helped anybody new to squeak/smalltalk.
Right, in this case not, but that's not what I meant primarily. Should have changed the subject and moved to squeak-dev, sorry. I'll make up for it, see below [*].
I meant that I've never seen a graphical explanation of what Randal did in these "few minutes" to find out. It's what experienced Smalltalkers do all the time, and Squeak provides powerful ways of finding out about itself, but they are not obvious to newbies.
Usually we don't even mention that it took us a minute to find out, so beginners get the impression that it's "all in our heads". It's not (not *all* anyway) but the tools allow us to find out quickly. Faster than looking up documentation indeed, which we can't really proof because of the lack of it ;)
Cookbook wiki maybe?
2009/8/25 Bert Freudenberg bert@freudenbergs.de
On 25.08.2009, at 23:31, Randal L. Schwartz wrote:
There might be a far better way.. but I found this in a few minutes.
Someone should make a Squeak version of
http://xkcd.com/627/
- Bert -
[*] Here's what I'd do:
Make a little test case in a workspace:
| dict | dict := {'a' -> 'first'. 'b' -> 'second'. 'c' ->'third'} as: Dictionary. dict
This intentionally does not use integers as keys, because that's what's special about Dictionaries. (and in fact I didn't really open a workspace but typed it right in the browser I happened to have open. Saves a second)
When I printed the expression above, the elements *were* sorted by key. So this may be chance, but I wanted to see. So I double-clicked Dictionary, pressed cmd-b to bring up a browser, clicked in the right pane with all the methods, and pressed p, to find printing-related methods (I should have clicked the "printing" protocol but usually methods are named more sanely than they are categorized). Anyway this took me right to the "printElementsOn: aStream" which indeed sorts. That could be a template for your own solution.
But in any case the print string of the dictionary is not helpful to show the problem. So:
| dict | dict := {'a' -> 'first'. 'b' -> 'second'. 'c' ->'third'} as: Dictionary. dict asArray
Yes, this printed #('second' 'third' 'first'), so now we need to sort. I have the idiom in my head but I typed in in anyway to verify
| dict | dict := {'a' -> 'first'. 'b' -> 'second'. 'c' ->'third'} as: Dictionary. dict keys asSortedCollection collect: [:key | dict at: key]
And this indeed gives an OrderedCollection('first' 'second' 'third')
So if I had sent an answer to your question I probably would have just pasted the line
dict keys asSortedCollection collect: [:key | dict at: key]
not even mentioning that it took me a minute to verify, and that en passant I discovered that dictionary printing is sorted for a couple of years already, which I had not noticed ;)
Comparing this to Randal's suggestion I'd say that mine feels more "Smalltalky". Accessing the associations in a dictionary directly feels unclean. Maybe because it breaks the abstraction, associations are an implementation detail of dictionaries. It's unlikely this will ever change and accessing the associations is fine, but still ;)
- Bert -
Beginners mailing list Beginners@lists.squeakfoundation.org http://lists.squeakfoundation.org/mailman/listinfo/beginners
beginners@lists.squeakfoundation.org