IComparer.Compare exception

This forum is meant for questions and discussions about the X# language and tools
Post Reply
JohnBonnett88
Posts: 57
Joined: Mon Mar 07, 2022 12:34 am

IComparer.Compare exception

Post by JohnBonnett88 »

Hi All,

I have had an odd problem in a VO X# system and have tracked it down to an ASort call that is failing. I have not so far been able to pin down the problem. I am sorting an array of 2-element arrays and it is sorting by comparing the first element of each of those sub-arrays. I think there are better ways of doing what the code is trying to do but, although the data has a fair bit of duplication, sorting should still work.

I made a small X# WinForms app, VO dialect, and replaced the programs.prg with the following code for testing:

Code: Select all

USING System
USING System.Collections.Generic
USING System.Linq
USING System.Text
USING System.Windows.Forms

USING TestFWXS

[STAThread] ;
FUNCTION Start() AS VOID
    LOCAL T, S AS ARRAY
    LOCAL I AS DWORD
    
    T := {{1,27},{2,6},{1,27},{2,6},{1,27},{2,6},{1,27},{2,6},{1,27},{2,6},{1,27},{2,6},{1,27},{2,6},{1,28},{2,3},{1,28},{2,3},{1,27},{2,6},{1,27},{2,6}}    
    FOR I := 1 UPTO ALen(T)
        S := ASize(AClone(T), I)
        ASort(S, , , {|x, y| x[1] <= y[1]})
    NEXT
    RETURN
The original problem occurred with an 18 element array and the array T above is an extended version of that. I copied the first 4 elements onto the end to extend it, but otherwise the data is the same. The code creates subsets of that T array and tries to sort them. All goes well until you get to 17 elements when it fails with:
System.ArgumentException
HResult=0x80070057
Message=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'.
Source=mscorlib
StackTrace:
at System.Collections.Generic.IntrospectiveSortUtilities.ThrowOrIgnoreBadComparer(Object comparer)
at System.Collections.Generic.GenericArraySortHelper`1.Sort(T[] keys, Int32 index, Int32 length, IComparer`1 comparer)
at System.Array.Sort[T](T[] array, Int32 index, Int32 length, IComparer`1 comparer)
at System.Collections.Generic.List`1.Sort(Int32 index, Int32 count, IComparer`1 comparer)
at XSharp.__Array.Sort(Int32 startIndex, Int32 count, IComparer`1 comparer)
at XSharp.RT.Functions.ASort(__Array aTarget, __Usual nStart, __Usual nCount, __Usual cbOrder)
at TestFWXS.Exe.Functions.Start() in C:\Users\XVJBONNE\source\repos\LabPro\TestFWXS\Program.prg:line 17
It is complaining that the comparer defined in the code block is faulty. It seems to fail with 17 or more elements. I will try over the weekend to debug it with the GitHub code for ASort to see if I can refine it further.

Any suggestions?

John Bonnett
JohnBonnett88
Posts: 57
Joined: Mon Mar 07, 2022 12:34 am

Re: IComparer.Compare exception

Post by JohnBonnett88 »

Just a follow-up to my previous message, in case it matters, I am running:

Microsoft Visual Studio Professional 2022 (64-bit) - Current Version 17.13.6
X# Visual Studio Project System - 2.21.0.5
Windows 11 Enterprise Version 22H2 OS build 22621.5189
Intel(R) Core(TM) i7-10850H CPU @ 2.70GHz 2.71 GHz
HansjoergP
Posts: 155
Joined: Mon Jul 25, 2016 3:58 pm
Location: Italy

Re: IComparer.Compare exception

Post by HansjoergP »

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

Re: IComparer.Compare exception

Post by robert »

John
This is fixed in the runtime update.
It will also be included in the upcoming new release.

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
JohnBonnett88
Posts: 57
Joined: Mon Mar 07, 2022 12:34 am

Re: IComparer.Compare exception

Post by JohnBonnett88 »

Thanks for everyone's replies.

I have applied the workaround suggested in the GitHub issue and my test example now works. I have applied the same fix as required in my main solution.

I must remember to check GitHub before bothering people on this forum!

Thanks again,
John
User avatar
robert
Posts: 4882
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Re: IComparer.Compare exception

Post by robert »

John,
JohnBonnett88 wrote: Wed Apr 30, 2025 1:02 am Thanks for everyone's replies.

I have applied the workaround suggested in the GitHub issue and my test example now works. I have applied the same fix as required in my main solution.

I must remember to check GitHub before bothering people on this forum!
I am not sure if you are using X# for your business or hobby.
If it is for your business, then why not support the project and become a subscriber?
That would have saved you some time, and you would also show your support for this project that has helped you move your VO application forward.

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