Home Segments Index Top Previous Next

431: Mainline

To create a sorted collection in which the elements are sorted in other than ascending numerical order, you send a sortBlock: message to SortedCollection. The block following the sortBlock: selector is a two-parameter block that answers true if and only if the first element is to appear before the second. Thus, if [:x :y | x < y] is the sort block, you might as well create the sorted collection using new; if [:x :y | x > y] is the sort block, the elements are sorted in descending order.

Workspace
((SortedCollection sortBlock: [:x :y | x > y]) 
   add: 570; add: 720; add: 640; add: 720; yourself) 
  do: 
  [:element | Transcript show: element printString; space] 
Transcript 
720 720 640 570