Cincom

Primer : Collections (Sorted) (Part 1)


The Collection class (and all its sub-classes) is one feature of the Smalltalk language that makes it so powerful. Tedious lines of code that are written over and over again in other languages are just a single message in Smalltalk. Sometimes, the best way to learn about something is just to play with it. The SortedCollection is no exception.

This lesson introduces SortedCollections and describes some of their specific characteristics.

1. If VisualWorks is not already running, please start running it now.

2. From the VisualWorks main Launcher window, either click the last button on the Toolbar or select the menu option Tools>>Workspace.

3. In the Workspace, enter the following:

|mySort |
mySort := SortedCollection new.
mySort add: 'dog'.
mySort add: 'apple'.
mySort add: 'cat'.
mySort inspect.

4. Highlight all of this text, <Operate-Click> and select Do it.

Note that a new (inspector) window will appear and the caption of the window is a SortedCollection.

5. In the left pane of the Inspector window, click (highlight) the word self.

You should see SortedCollection ('apple' 'cat' 'dog')

6. In the Inspector window, click the Elements tab. In the left pane of the window, click (highlight) the 1.

You should see 'apple'

 

Figure S-1. Inspector Window of our
SortedCollection

 

Figure S-2. The "first" item in our SortedCollection


7. Close the Inspector window

How does a Sorted Collection work?

By definition, a SortedCollection is just that, a collection of whatever you put into it or whatever you decide to collect but its contents are sorted or are in order. For out example, since we are placing Strings into our collection, they are alphabetized. In short, it stores whatever you place in it in a sorted order. However, if you ever wanted to reverse that order, you would require what is called a sort block. Watch what happens when you compare the results (above) to the one below.

8. In the Workspace, enter the following:

|mySort |
mySort := SortedCollection sortBlock: [:a :b| a >= b].
mySort add: 'cat'.
mySort add: 'apple'.
mySort add: 'dog'.
mySort inspect.

9. Highlight all of this text, <Operate-Click> and select Do it.

Note that a new (Inspector) window will appear and the caption of the window is still "a SortedCollection".

10. In the left pane of the Inspector window, click (highlight) the word self.

You should see SortedCollection ('dog' 'cat' 'apple')

11. In the left pane of the Inspector window, click (highlight) the 1.

You should see 'dog'


Figure S-3. Inspector Window of our SortedCollection


Figure S-4. The "first" item in our SortedCollection



12. Close the Inspector window

Can you see how a SortedCollection works now?

On the first line, we declared a temporary variable (mySort). On the second line, we told Smalltalk to create a new SortedCollection and assign it to our temporary variable. On the third line, we added the String 'cat' to our SortedCollection. On the next line, we added the String 'apple' to our SortedCollection and finally added the String 'dog'. Then we told Smalltalk to inspect our SortedCollection.

 we add something to a SortedCollection, Smalltalk inserts it into the collection, in the sorted order. If none is given, it defaults to ascending. However, you override the default sort order by using a sort block. Try not to read too much into the syntax of the block [:a :b | a >= b]. Think of it like this: "Of any 2 items in the SortedCollection (a and b), make an item following another item less than or equal to the other". If you were to change the greater than sign to a less than sign, the sort order would be reversed. In most programming languages, you would have to write (sort) code yourself. In Smalltalk, it's built into the language.

Summary

SortedCollections are collections that keep items in a particular sequence or order.