Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Runtime Version 2.21.0.5 ASort with Codeblock - IComparer.Compare() method returns inconsistent results #1653

Open
christianschmid200272 opened this issue Dec 9, 2024 · 2 comments
Assignees
Milestone

Comments

@christianschmid200272
Copy link
Contributor

An example in which I have rebuilt the array with constant values, the error can also be reproduced with this.

FUNCTION Start() AS VOID STRICT
	LOCAL aPosListe     AS ARRAY
	aPosListe   := { }
	AAdd(aPosListe,{"Test", 1,		0, 0,		"29991231B", "  /  ", "  /  ", 0, 0, 0, 0})
	AAdd(aPosListe,{"Test", 1,		0, 0,		"29991231B", "  /  ", "  /  ", 0, 0, 0, 0})
	AAdd(aPosListe,{"Test", 1,		0, 0,		"29991231B", "  /  ", "  /  ", 0, 0, 0, 0})
	AAdd(aPosListe,{"Test", 0.5,	0, 0,		"29991231B", "  /  ", "  /  ", 0, 0, 0, 0})
	AAdd(aPosListe,{"Test", 6.5,	0, 0,		"29991231B", "  /  ", "  /  ", 0, 0, 0, 0})
	AAdd(aPosListe,{"Test", 1,		0, 0,		"29991231B", "  /  ", "  /  ", 0, 0, 0, 0})
	AAdd(aPosListe,{"Test", 0.5,	0, 0,		"29991231B", "  /  ", "  /  ", 0, 0, 0, 0})
	AAdd(aPosListe,{"Test", 4,		0, 0,		"29991231B", "  /  ", "  /  ", 0, 0, 0, 0})
	AAdd(aPosListe,{"Test", 1.8,	0, 0,		"29991231B", "  /  ", "  /  ", 0, 0, 0, 0})
	AAdd(aPosListe,{"Test", 7,		0, 0,		"29991231B", "  /  ", "  /  ", 0, 0, 0, 0})
	AAdd(aPosListe,{"Test", 2,		0, 0,		"29991231B", "  /  ", "  /  ", 0, 0, 0, 0})
	AAdd(aPosListe,{"Test", 470,	0, 0,		"29991231B", "  /  ", "  /  ", 0, 0, 0, 0})
	AAdd(aPosListe,{"Test", 1,		0, 0,		"29991231B", "  /  ", "  /  ", 0, 0, 0, 0})
	AAdd(aPosListe,{"Test", 1,		0, 0,		"20191007B", "40/19", "  /  ", 0, 0, 0, 0})
	AAdd(aPosListe,{"Test", 1,		0, 0,		"20191007B", "40/19", "  /  ", 0, 0, 0, 0})
	AAdd(aPosListe,{"Test", 1,		0, 0,		"20191007B", "40/19", "  /  ", 0, 0, 0, 0})
	AAdd(aPosListe,{"Test", 1,		0, 0,		"20191007B", "40/19", "  /  ", 0, 0, 0, 0})
	AAdd(aPosListe,{"Test", 1,		0, 0,		"20191007B", "40/19", "  /  ", 0, 0, 0, 0})
	AAdd(aPosListe,{"Test", 1,		0, 0,		"20191007B", "40/19", "  /  ", 0, 0, 0, 0})

	?ASort( aPosListe, , , {|x,y| x[5] <= y[5] } )

The error throwns in https://github.com/X-Sharp/XSharpPublic/blob/9b4a562c5a4d411be239f68fba9750f3d4313514/src/Runtime/XSharp.RT/Types/Array.prg#L468
, unfortunately I don't see why.

    NEW INTERNAL METHOD Sort(startIndex AS INT, count AS INT, comparer AS IComparer<__Usual>) AS VOID
        IF startIndex <= 0
            startIndex := 1
        ENDIF
        IF count < 0
            count := _internalList:Count - startIndex + __ARRAYBASE__
        ENDIF
        _internalList:Sort(startIndex-__ARRAYBASE__ ,count,comparer)
        RETURN
@RobertvanderHulst
Copy link
Member

This is caused by the new TargetFramework Attribute. The sorting routines in the Framework have changed in .Net Framework 4.5. Before this build, withouit the attribute, the Framework was using the "old" sorting routines. Now it is using the new sorting routines.

If you look here, then you'll see that the Sort algorithm has changed in .Net 4.5:
https://learn.microsoft.com/en-us/dotnet/framework/migration-guide/runtime/4.5.x

You will most likely see different results if there are keys that have the same value.
To work around this:

  • either make sure that the keys are unique
  • supress the generation of the attribute by adding a property to your project file, inside the first <PropertyGroup>
    <GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
    (this disables the build step that generates the attribute)

@RobertvanderHulst
Copy link
Member

We will document this change and the workaround

@RobertvanderHulst RobertvanderHulst moved this from To do to In progress in Build 2.x Dec 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: In progress
Development

No branches or pull requests

2 participants