Asort using codeblocks errors
Posted: Mon Apr 12, 2021 10:34 am
Hi
I have an issue with "asort" and using a codeblock, where in some instances i get the error. (there are quite a few posts on google showing the same kind of error)
"Exception raised by CLR or external code:Unable to sort because the IComparer.Compare() method returns inconsistent results. Either a value does not compare equal to itself, or one value repeatedly compared to another value yields different results. IComparer: 'XSharp.RT.ArraySortComparer'"
My example i'm using : asort({|x,y| x[2]<=y[2] })
I noticed that in ArraySortComparer that ASort uses (your code below), you are comparing the array (object) to see if it's the same, and not the individual element of the array (which i understand you can't do, as the element is not known as stored in the codeblock)
I guess the only solution would be for the codeblock to return 0 if they are the same e.g. in the below example. (this would obvious break all Asort Codeblocks, but could be a solution !)
{|x,y| iif(x[2]==y[2],0,iif(x[2]<=y[2],-1,1)) }
In the mean time, i'm going back to using "AQuickSort" which i got from the forum many years ago.
STRUCTURE ArraySortComparer<T, U> IMPLEMENTS System.Collections.Generic.IComparer<T> WHERE T IS NEW()
PRIVATE _cb AS @@Func<T,T,LOGIC>
CONSTRUCTOR( cb AS @@Func<T,T,LOGIC> )
_cb := cb
RETURN
METHOD Compare( x AS T, y AS T ) AS INT
IF Object.ReferenceEquals(x, y )
RETURN 0
ENDIF
LOCAL u AS LOGIC
u := _cb( x, y )
RETURN IIF ( u , -1, 1 )
https://docs.microsoft.com/en-us/dotnet ... ew=net-5.0
Thanks, Ian
I have an issue with "asort" and using a codeblock, where in some instances i get the error. (there are quite a few posts on google showing the same kind of error)
"Exception raised by CLR or external code:Unable to sort because the IComparer.Compare() method returns inconsistent results. Either a value does not compare equal to itself, or one value repeatedly compared to another value yields different results. IComparer: 'XSharp.RT.ArraySortComparer'"
My example i'm using : asort({|x,y| x[2]<=y[2] })
I noticed that in ArraySortComparer that ASort uses (your code below), you are comparing the array (object) to see if it's the same, and not the individual element of the array (which i understand you can't do, as the element is not known as stored in the codeblock)
I guess the only solution would be for the codeblock to return 0 if they are the same e.g. in the below example. (this would obvious break all Asort Codeblocks, but could be a solution !)
{|x,y| iif(x[2]==y[2],0,iif(x[2]<=y[2],-1,1)) }
In the mean time, i'm going back to using "AQuickSort" which i got from the forum many years ago.
STRUCTURE ArraySortComparer<T, U> IMPLEMENTS System.Collections.Generic.IComparer<T> WHERE T IS NEW()
PRIVATE _cb AS @@Func<T,T,LOGIC>
CONSTRUCTOR( cb AS @@Func<T,T,LOGIC> )
_cb := cb
RETURN
METHOD Compare( x AS T, y AS T ) AS INT
IF Object.ReferenceEquals(x, y )
RETURN 0
ENDIF
LOCAL u AS LOGIC
u := _cb( x, y )
RETURN IIF ( u , -1, 1 )
https://docs.microsoft.com/en-us/dotnet ... ew=net-5.0
Thanks, Ian