Re: The Inbox: Collections-ct.947.mcz
Posted by
Levente Uzonyi on
Jun 12, 2021; 7:52pm
URL: https://forum.world.st/The-Inbox-Collections-ct-947-mcz-tp5130322p5130364.html
On Thu, 10 Jun 2021,
[hidden email] wrote:
> Hi Levente,
>
>> #sort: and #sorted: expect an argument which responds to #value:value: with a boolean, so both of those examples work if you don't send #asCompareSortFunction to the block.
>>
>> However, #asCompareSortFunction may be useful if you want to create chained sort functions. I'm not sure about the name though.
>
> Ouch! I did not see the wood for the trees. :-) But yes, if you would like to, for example, invert the order of a compare sort block, the protocols would not match ... So I guess this would still be relevant.
It's not clear to me what you mean by "invert the order of a compare sort
block.".
However, I see one problem with the implementation. It evaluates the block
but lies about equality. It only returns -1 or 1, never 0. Which means,
chaining is impossible.
Here's an example:
#((1 a) (1 b)) sorted: [ :a | a first ] ascending, [ :a | a second ] descending
"==> #(#(1 #b) #(1 #a)) which is fine"
but
#((1 a) (1 b)) sorted: [ :a :b | a first <= b first ] asCompareSortFunction, [ :a | a second ] descending.
"==> #(#(1 #a) #(1 #b)) which is not what I would expect"
Levente