Hi Otto

I think the main reason is to support the second usage scenario:
SequenceableCollection withAllSubclasses sortedAs: [:class | class
methodDict size].
We could try and unify the sort: method to take only blocks while sortedAs:
a symbol but then at the same time we could just put everything into one
method and use cull: perhaps to handle all the scenarios.

Personally I don’t feel too comfortable with the code below, apart from the
performance penalties it also feels like code that is too ‘clever’ for
itself which obviously makes it complicated to understand and modify. I
wrote it mainly for people to voice opinions and get ideas flowing. I’d have
2 separate sort methods so that users can choose if they want the runtime
characteristics of either.

Example which can handle all current scenarios i.e. symbol or block taking 1
or 2 arguments:
SequenceableCollection>>sort: aSortBlock 
        self
                mergeSortFrom: 1
                to: self size
                by: (self createSortBlock:  aSortBlock)


SequenceableCollection>>createSortBlock: aSortBlockOrSymbol 
        ^ aSortBlockOrSymbol numArgs < 2
                ifTrue: [[ :a :b | (#(true false )
                                        includes: (aSortBlockOrSymbol value: a))
                                ifTrue: [ aSortBlockOrSymbol value: a]
                                ifFalse: [(aSortBlockOrSymbol value: a) < 
(aSortBlockOrSymbol value:
b)]]]
                ifFalse: [[:a :b | aSortBlockOrSymbol cull: a cull: b]] 


BTW the previous versions had a bug where it fails when called on an empty
collection as the true/false check is done eagerly.

Cheers
Carlo



--
View this message in context: 
http://forum.world.st/Sort-by-property-tp4748500p4748757.html
Sent from the Pharo Smalltalk Developers mailing list archive at Nabble.com.

Reply via email to