[Newbies] how to sort by key a Dictionary object?

Bert Freudenberg bert at freudenbergs.de
Wed Aug 26 08:30:17 UTC 2009


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 at 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 -
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.squeakfoundation.org/pipermail/beginners/attachments/20090826/ba39d395/attachment-0001.htm


More information about the Beginners mailing list