Unexpected behaviour with stack.Sort(fn) and the "-" Operator ?
Closed this issue · 2 comments
abakobo commented
I used to use the "-" operator to compare into std.collection.Stack.Sort() Lambdas but I met some unexpected sorting and will now use the "<=>" operator. But is this normal?
The following code prints a wrongly sorted stack followed by a correctly sorted stack made with a sort resp. using the - operator followed by the <=> operator.
#Import "<std>"
Using std..
Function Main()
Local st:=New Stack<Double>
st.Add(359.441293598843)
st.Add(330.80808897393865)
st.Add(359.2445636070442)
st.Sort(Lambda:Int(a:Double,b:Double)
Return a - b
End ) 'doesn't work with a-b
For Local v:=Eachin st
Print v
Next
Print "---"
st.Sort(Lambda:Int(a:Double,b:Float)
Return a <=> b
End ) 'works with a<=>b
For Local d:=Eachin st
Print d
Next
End
blitz-research commented
I used to use the "-" operator to compare into .Sort Lambdas but I met
some unexpected sorting and will now use the "<=>" operator. But is this
normal?
This totally depends on what you mean by 'unexpected sorting', which you
have not described. Running the code, the first bunch of vec2's are sorted
by x coordinate, which is exactly what your code appears to be doing so I
don't find that at all unexpected!
The Vec2 '<=>' operator provides an arbitrary but constistent (eg: if x<y
then y>=x) ordering as it takes both x and y into account. This allows you
to use Vec2s as Map keys etc. The actually ordering of the Vec2s will be
kind of meangingless, but it will allow you to find them in a map very
quickly etc.
Note that your first lamba is actually very dodgy, if it is used with 2 x
values that have the same integer part but different fractional parts, it
will return '0' which Sort() takes to mean '='.
But depending on what you're actually doing (and I have no idea) I would
probably recommend using the '<=>' operator.
…On Sat, Jan 19, 2019 at 8:42 AM abakobo ***@***.***> wrote:
I used to use the "-" operator to compare into .Sort Lambdas but I met
some unexpected sorting and will now use the "<=>" operator. But is this
normal?
Namespace myapp
#Import "<std>"
Using std..
Alias Vec2d:Vec2<Double>
Function Main()
Local st:=New Stack<Vec2d>
st.Add(New Vec2d(359.441293598843,276.18055418899803))
st.Add(New Vec2d(330.80808897393865,255.86331683660833))
st.Add(New Vec2d(359.2445636070442,276.04096066497334))
st.Sort(Lambda:Int(a:Vec2d,b:Vec2d)
Return a.x - b.x
End ) 'doesn't work with a-b
For Local v:=Eachin st
Print v
Next
Print "---"
st.Sort(Lambda:Int(a:Vec2d,b:Vec2d)
Return a.x <=> b.x
End ) 'works with a<=>b
For Local v:=Eachin st
Print v
Next
End
Prints a wrongly sorted stack followed by a correctly sorted stack..
Thx & best regards.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#459>, or mute the thread
<https://github.com/notifications/unsubscribe-auth/ADU3QoaEaKHIzKU4nfkURHB20rfptvGdks5vEiOJgaJpZM4aIhfA>
.
abakobo commented
Yes of course, the lambda returns an Int.. I've been lucky since then not
to meet a bug. I'll probably add an example in the docs at some point..
Thanks again.
Le sam. 19 janv. 2019 à 00:30, Mark Sibly <notifications@github.com> a
écrit :
… > I used to use the "-" operator to compare into .Sort Lambdas but I met
some unexpected sorting and will now use the "<=>" operator. But is this
normal?
This totally depends on what you mean by 'unexpected sorting', which you
have not described. Running the code, the first bunch of vec2's are sorted
by x coordinate, which is exactly what your code appears to be doing so I
don't find that at all unexpected!
The Vec2 '<=>' operator provides an arbitrary but constistent (eg: if x<y
then y>=x) ordering as it takes both x and y into account. This allows you
to use Vec2s as Map keys etc. The actually ordering of the Vec2s will be
kind of meangingless, but it will allow you to find them in a map very
quickly etc.
Note that your first lamba is actually very dodgy, if it is used with 2 x
values that have the same integer part but different fractional parts, it
will return '0' which Sort() takes to mean '='.
But depending on what you're actually doing (and I have no idea) I would
probably recommend using the '<=>' operator.
On Sat, Jan 19, 2019 at 8:42 AM abakobo ***@***.***> wrote:
> I used to use the "-" operator to compare into .Sort Lambdas but I met
> some unexpected sorting and will now use the "<=>" operator. But is this
> normal?
>
> Namespace myapp
> #Import "<std>"
> Using std..
>
> Alias Vec2d:Vec2<Double>
>
> Function Main()
>
> Local st:=New Stack<Vec2d>
> st.Add(New Vec2d(359.441293598843,276.18055418899803))
> st.Add(New Vec2d(330.80808897393865,255.86331683660833))
> st.Add(New Vec2d(359.2445636070442,276.04096066497334))
> st.Sort(Lambda:Int(a:Vec2d,b:Vec2d)
> Return a.x - b.x
> End ) 'doesn't work with a-b
> For Local v:=Eachin st
> Print v
> Next
> Print "---"
> st.Sort(Lambda:Int(a:Vec2d,b:Vec2d)
> Return a.x <=> b.x
> End ) 'works with a<=>b
> For Local v:=Eachin st
> Print v
> Next
> End
>
> Prints a wrongly sorted stack followed by a correctly sorted stack..
>
> Thx & best regards.
>
> —
> You are receiving this because you are subscribed to this thread.
> Reply to this email directly, view it on GitHub
> <#459>, or mute the
thread
> <
https://github.com/notifications/unsubscribe-auth/ADU3QoaEaKHIzKU4nfkURHB20rfptvGdks5vEiOJgaJpZM4aIhfA
>
> .
>
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#459 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ANBkSTTBHcoImHa2sZFftKUNipmtR_Sgks5vElkugaJpZM4aIhfA>
.