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

Randal L. Schwartz merlyn at stonehenge.com
Tue Aug 25 21:31:30 UTC 2009


>>>>> "r00t" == r00t uk <r00tuk at 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.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn at stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion


More information about the Beginners mailing list