Hello Squeakers!
I've been dazed and confused ((C)Led Zeppelin) by SortedCollection.
| collection | collection := SortedCollection new. collection sortBlock: [:element1 :element2 | element1 > element2]. 1 to: 10 do: [:i | collection add: i]. Transcript cr; show: (collection copyFrom: 1 to: 5) printString
Got SortedCollection (6 7 8 9 10 ), although expected SortedCollection (10 9 8 7 6 ). I think that SortedCollection (6 7 8 9 10 ) has sortBlock: [:x :y | x <= y] (default) *not* [:x :y | x > y] (required). Is it right?
Here is my first aid.
'From Squeak 2.0 of May 22, 1998 on 29 August 1998 at 3:43:36 am'!
!SortedCollection methodsFor: 'copying' stamp: 'nishis 8/29/1998 03:43'! copyFrom: startIndex to: endIndex "by nishis, 1998/08/29 03:42"
| targetCollection | targetCollection := super copyFrom: startIndex to: endIndex. targetCollection sortBlock: sortBlock. ^targetCollection! !
regards
It's late, but here goes...
I've been dazed and confused ((C)Led Zeppelin) by SortedCollection.
Transcript cr; show: (collection copyFrom: 1 to: 5) printString
Got SortedCollection (6 7 8 9 10 ), although expected SortedCollection (10 9 8 7 6 ).
Because there are a number of *copy* methods that do not give SortedCollection a chance to set the sortBlock for the new collection.
Here is my first aid.
targetCollection sortBlock: sortBlock.
This will resort the collection using your desired sortBlock. It is inefficient, but it will work. Better to set the sortBlock before adding elements. For example (same could be applied to a number of other copy methods)...
OrderedCollection>>copyFrom: start to: end
| copy | end < start ifTrue: [^self copyEmpty]. copy := self copyEmptySize: end -start + 1. start to: end do: [ :n | copy add: (self at: n)]. ^copy
....where...
OrderedCollection>>copyEmptySize: n
^self species new: n
....and...
SortedCollection>>copyEmptySize: n
^(self species new: n) sortBlock: self sortBlock; yourself
Untested because I have to get back to testing Java... 8*(
squeak-dev@lists.squeakfoundation.org