Object subclass: #Sorter instanceVariableNames: 'accessors ' classVariableNames: '' poolDictionaries: ''! Sorter comment: 'Instances may be used in place of a sort block.'! !Sorter publicMethodsFor: 'accessing'! accessors ^accessors! accessors: aCollection accessors := aCollection! ! !Sorter publicMethodsFor: 'initialize-release'! initialize "set up the accessor collection" self accessors: OrderedCollection new! ! !Sorter publicMethodsFor: 'private'! value: v1 value: v2 comparator: anInteger "use an accessor to compare v1 & v2" | accessor ascending c1 c2 | accessors size < anInteger ifTrue: ["no more accessors; return true (don't change order)" ^true]. accessor := (accessors at: anInteger) key. ascending := (accessors at: anInteger) value. ^(c1 := v1 perform: accessor) = (c2 := v2 perform: accessor) ifFalse: [ascending ifTrue: [c1 < c2] ifFalse: [c1 > c2]] ifTrue: [self value: v1 value: v2 comparator: anInteger + 1]! ! !Sorter publicMethodsFor: 'setup'! addAscendingSelector: aSymbol self accessors add: aSymbol -> true! addDescendingSelector: aSymbol self accessors add: aSymbol -> false! ! !Sorter publicMethodsFor: 'sort block behavior'! value: v1 value: v2 "imitate a sort block by answering the message value:value:" ^self value: v1 value: v2 comparator: 1! ! "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "! !Sorter class publicMethodsFor: 'examples'! example1 "self example1" | col sorter | col := OrderedCollection new add: Date today; add: (Date readFrom: 'July 4, 1776' readStream); add: (Date readFrom: 'January 3, 1959' readStream); add: (Date readFrom: 'December 25, 1776' readStream); yourself. sorter := self new addAscendingSelector: #year; addAscendingSelector: #monthName; yourself. (col asSortedCollection: sorter) inspect.! ! !Sorter class publicMethodsFor: 'instance creation'! new ^super new initialize! !