geoff
2011-01-13 04:51:23 UTC
I wanted to try different sorts with SortedCollection. To change the
sorting method, I modified 'SortedCollection>>#addAllSelectionSorted:'. The
complete method for a selection sort is:
addAllSelectionSorted: aCollection
"Answer aCollection having added each element of aCollection to the
receiver."
| arr currentIndex collSize iMin i temp |
elements := (elements asOrderedCollection addAll: aCollection) asArray.
size := elements size.
arr := elements.
currentIndex := 1.
collSize := arr size.
[ currentIndex < collSize ] whileTrue: [
iMin := currentIndex.
i := currentIndex + 1.
[ i <= collSize ] whileTrue: [
(sortBlock value: (arr at: iMin) value: (arr at: i))
ifTrue: [ iMin := i ].
i := i + 1.
].
temp := arr at: currentIndex.
arr at: currentIndex put: (arr at: iMin).
arr at: iMin put: temp.
currentIndex := currentIndex + 1.
].
^aCollection
. . . I noticed that 'SortedCollection>>#addAll:' also uses the default
sort. If one subclassed SortedCollection, I'm curious what methods others
would override to implement a new sort?
--g
sorting method, I modified 'SortedCollection>>#addAllSelectionSorted:'. The
complete method for a selection sort is:
addAllSelectionSorted: aCollection
"Answer aCollection having added each element of aCollection to the
receiver."
| arr currentIndex collSize iMin i temp |
elements := (elements asOrderedCollection addAll: aCollection) asArray.
size := elements size.
arr := elements.
currentIndex := 1.
collSize := arr size.
[ currentIndex < collSize ] whileTrue: [
iMin := currentIndex.
i := currentIndex + 1.
[ i <= collSize ] whileTrue: [
(sortBlock value: (arr at: iMin) value: (arr at: i))
ifTrue: [ iMin := i ].
i := i + 1.
].
temp := arr at: currentIndex.
arr at: currentIndex put: (arr at: iMin).
arr at: iMin put: temp.
currentIndex := currentIndex + 1.
].
^aCollection
. . . I noticed that 'SortedCollection>>#addAll:' also uses the default
sort. If one subclassed SortedCollection, I'm curious what methods others
would override to implement a new sort?
--g