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

"Failed to compare two elements in the array." System.InvalidOperationException (V1.2.19 compared to V1.2.18) #617

Closed
michaeldye79 opened this issue Aug 2, 2022 · 11 comments
Assignees
Labels

Comments

@michaeldye79
Copy link

michaeldye79 commented Aug 2, 2022

Hello all,

We've come across a [new] exception being thrown having upgraded V1.2.18 >> V1.2.19:

Exception: System.InvalidOperationException: Failed to compare two elements in the array.
 ---> System.EntryPointNotFoundException: Entry point was not found.
   at System.Collections.IComparer.Compare(Object x, Object y)
   at System.Collections.Generic.IComparer`1\[\[System.DateTime\, System.Private.CoreLib\, Version=6.0.0.0\, Culture=neutral\, PublicKeyToken=7cec85d7bea7798e\]\]_OURAPI.Utilities.AlphanumComparator.Compare(DateTime , DateTime )
   at System.Linq.EnumerableSorter`2.CompareAnyKeys(Int32 index1, Int32 index2)
   at System.Collections.Generic.ArraySortHelper`1.InsertionSort(Span`1 keys, Comparison`1 comparer)
   at System.Collections.Generic.ArraySortHelper`1.IntroSort(Span`1 keys, Int32 depthLimit, Comparison`1 comparer)
   at System.Collections.Generic.ArraySortHelper`1.Sort(Span`1 keys, Comparison`1 comparer)
   --- End of inner exception stack trace ---
   at System.Collections.Generic.ArraySortHelper`1.Sort(Span`1 keys, Comparison`1 comparer)
   at System.MemoryExtensions.Sort[T](Span`1 span, Comparison`1 comparison)
   at System.Linq.EnumerableSorter`2.QuickSort(Int32[] keys, Int32 lo, Int32 hi)
   at System.Linq.EnumerableSorter`1.Sort(TElement[] elements, Int32 count)
   at System.Linq.OrderedEnumerable`1.GetEnumerator()+MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at OURAPI.Pagination.PaginationSort.ApplySort[T](SortedResults`1 results, String orderByQueryString, String defaultSort)

As in; it works without a hiccup in V1.2.18; but throws the above in V1.2.19.

The code in question is:

results.Data = d.OrderBy(orderQueryBuilder.ToString(), new AlphanumComparator()).ToList();

where our alpha numeric comparator is defined as:

public class AlphanumComparator : System.Collections.Generic.IComparer<object>

Not sure what we're doing wrong; the order by query can indeed be for string items, date/time, numeric etc. But thought this was the correct approach to provide a comparator that'd sort out alphanumeric strings.

Any help appreciated - ta, Dr Michael Dye.

@StefH StefH self-assigned this Aug 2, 2022
@StefH
Copy link
Collaborator

StefH commented Aug 2, 2022

Can you try to extend AlphanumComparator from IComparer?

Like is done here:
https://github.com/zzzprojects/System.Linq.Dynamic.Core/blob/master/test/System.Linq.Dynamic.Core.Tests/QueryableTests.OrderBy.cs#L43

@StefH
Copy link
Collaborator

StefH commented Aug 2, 2022

Note that in the version v1.2.18, the OrderBy did not use the comparer correctly, this was fixed in:

@michaeldye79
Copy link
Author

Thanks for the very quick reply.

100% converting to IComparer without the <> gets rid of the crash / exception.

However; and I really don't get this - the sort doesn't work!

If I do the method call without providing a comparer:

results.Data = d.OrderBy(orderQueryBuilder.ToString()).ToList();

That works [but it's alphabetic not alphanumeric]

If I do:

results.Data = d.OrderBy(orderQueryBuilder.ToString(), new AlphanumComparer()).ToList();

It runs the comparer (can breakpoint that) but it's always receiving large integers to sort:
X: 610996776 Y: 610996416
X: -468412496 Y: -468412496
X: 610997160 Y: 610998320
X: -468412496 Y: -468412496
X: 610997160 Y: 610997944
X: -468412496 Y: -468412496
X: 610997160 Y: 610996776

Dunno where they come from!

If I use the sort routine from elsewhere (i.e. not dynamic LINQ) it works; receives strings and does what it should.

Sorry! Not sure what I'm not doing that I should do...

The simple mod I made to the sort code was:

/// <summary>
/// Alphanumeric comparison (for dynamic LINQ)
/// </summary>
public class AlphanumComparer : IComparer
{
    public int Compare(object? x, object? y)
    {
        return new AlphanumComparison().Compare(x, y);
    }
}
/// <summary>
/// Alphanumeric string comparison
/// </summary>
public class AlphanumComparison : IComparer<object?>
{
    public int Compare(object? x, object? y)
    {

... the sort code

(tried to model this after your test code and the Microsoft help...)

Ta, Dr. Michael Dye

@StefH
Copy link
Collaborator

StefH commented Aug 2, 2022

@michaeldye79
Can you provide a full working console-app project ?

@michaeldye79
Copy link
Author

Hi - is this any good?

DynamicSortTest.zip

@StefH
Copy link
Collaborator

StefH commented Aug 2, 2022

Yes. I think I did find the bug, now I need to solve it...

@StefH
Copy link
Collaborator

StefH commented Aug 2, 2022

@michaeldye79

I think I solved it, see this PR: #619

Can you download this branch and test it? Or do you need a MyGet preview version to test?

@michaeldye79
Copy link
Author

Wow thanks... Not done branch download before but will try first thing in the morning (GMT+1) and let you know. Ta.

@StefH
Copy link
Collaborator

StefH commented Aug 3, 2022

@michaeldye79
Copy link
Author

Morning! Yes - definitely fixed - the sort works fine. Brill; thanks.

As we're still developing I'll use this patch if that's ok until the next "proper" release happens?

Ta, Michael.

@StefH
Copy link
Collaborator

StefH commented Aug 3, 2022

@michaeldye79
Thanks for verifying.

For now you can indeed use this preview version, an official version will be released in some days...

@StefH StefH closed this as completed Aug 3, 2022
@StefH StefH added the feature label Oct 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants