"sortBlock is supposed to act like #<=" means "for every triple of elements x y z that might be in the collection, b(x,x) is true b(x,y) is Boolean b(x,y) | b(y,x) b(x,y) & b(y,z) implies b(x,z)." The first condition distinguishes it from #< . In particular, if you want to sort a sequence of *identical* elements, the sortBlock must satisfy b(x,x).
There are other predicates around that act like <= in the relevant sense: >= for example. But #beginsWith: and #endsWith: don't satisfy dichotomy. On 24 April 2018 at 02:43, Erik Stel <erik.s...@gmail.com> wrote: > Richard, > > Can you explain me what you mean by "sortBlock is supposed to act like > #<="? > Isn't it up to the developer to decide what the logic should be? I simply > used #<= because #> might not have been implemented for all relevant > classes, but would otherwise have chosen #> as a means to get the > 'reversal' > of #<=. > > Your solution is not resulting in the behaviour I would expect it. Adding > elements which are compared equally, but still have a different value, do > not get added in the same position. In the examples below I use > Associations, since it compares on the key only (for #<= comparison). > > Consider a regular SortedCollection with default sortBlock (set > explicitly): > | aCollection | > aCollection := SortedCollection sortBlock: [ :a :b | a <= b ]. > {#k->'Some'. #k->'Value'. #k->'Or'. #k->'Other'} do: [ :each | aCollection > add: each ]. > aCollection. > "a SortedCollection(#k->'Some' #k->'Value' #k->'Or' #k->'Other')" > > When I create a SortedCollection with your code's sortBlock reversed I get: > | aCollection | > aCollection := SortedCollection sortBlock: [ :a :b | b <= a ]. > {#k->'Some'. #k->'Value'. #k->'Or'. #k->'Other'} do: [ :each | aCollection > add: each ]. > aCollection. > "a SortedCollection(#k->'Some' #k->'Value' #k->'Or' #k->'Other')" > > The order of the result is the same in both situations. What I would expect > is the result you get from the sortBlock I suggested: > | aCollection | > aCollection := SortedCollection sortBlock: [ :a :b | (a <= b) not ]. > {#k->'Some'. #k->'Value'. #k->'Or'. #k->'Other'} do: [ :each | aCollection > add: each ]. > aCollection. > "a SortedCollection(#k->'Other' #k->'Or' #k->'Value' #k->'Some')" > > This last result is the reversal of the original result. > > (side note) > IF in the above #addAll: was used instead of the repeated #add:, things > might be different again. Since the #addAll: implementation would on some > occasions (when size of receiver vs size of added elements is certain > ratio) > add the elements at the end and then perform a #reSort. With values being > compared as equal, the order will be decided by the order they were added. > So the result of #addAll: depends on the collection sizes. This might not > be > what a user would expect ;-). > > > > -- > Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html > >