Asort using codeblocks errors

This forum is meant for questions and discussions about the X# language and tools
Post Reply
IanB
Posts: 9
Joined: Tue Nov 01, 2016 8:12 pm
Location: UK

Asort using codeblocks errors

Post by IanB »

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
User avatar
robert
Posts: 4518
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Asort using codeblocks errors

Post by robert »

Ian,
I think the solution to the problem is to change

Code: Select all

IF Object.ReferenceEquals(x, y )
RETURN 0
ENDIF
to

Code: Select all

IF Object.Equals(x, y )
RETURN 0
ENDIF
to make sure that when x and y point to a different memory location but have the same value they are also treated as "Equal".

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
IanB
Posts: 9
Joined: Tue Nov 01, 2016 8:12 pm
Location: UK

Asort using codeblocks errors

Post by IanB »

Hi Robert

Thanks for your quick reply. I thought you would need to compare the actual Element, but i guess if this works then i could try.

Thanks, Ian
User avatar
robert
Posts: 4518
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Asort using codeblocks errors

Post by robert »

Ian,

Can you mail an example of what you were testing to make sure that it works ?

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
Post Reply