Uncaught exception in SfDataGrid SortComparer

Hi syncfusion team,

we have a DateTime Column in german date format (dd.mm.yyyy) and sort it via a custom SortComparer like in your example (https://www.syncfusion.com/kb/6223/how-to-group-a-column-with-datetime-values-without-showing-its-time-part-in-sfdatagrid)

We've got a support case today, that the app crashes after sorting descending (by clicking 2 times on the date column header). I am able to reproduce this case with a minimum of 20 items in datagrid, all with the same date. In the customers case there are 400 items, many of them with the similar date.

The exception occures after the last function call of the sort comparer function. 

QueryableCollectionRecordsList.PopulateRecords (System.Collections.IEnumerable source, Syncfusion.Data.CollectionViewAdv view)

System.ArgumentException: 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: 'System.Collections.Generic.ComparisonComparer`1[System.Int32]'.


Can you try to reproduce it? 
Its syncfusion 17.4.0.55

Thank you!

Regards,
Maria

3 Replies

PK Pradeep Kumar Balakrishnan Syncfusion Team May 27, 2020 12:01 PM UTC

Hi Maria, 
 
Thank you for contacting Syncfusion support. 
 
We have checked the reported crash issue “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: 'System.Collections.Generic.ComparisonComparer`1[System.Int32]'.” We able to reproduce the reported issue. Please do the following changes in compare method to resolve this issue. 
 
Code snippet: 
public int Compare(object x, object y) 
{ 
    DateTime dateX = DateTime.MaxValue; 
    DateTime dateY = DateTime.MaxValue; 
 
    if (x.GetType() == typeof(OrderInfo)) 
    { 
        dateX = ((OrderInfo)x).ShippingDate; 
        dateY = ((OrderInfo)y).ShippingDate; 
    } 
    else if (x.GetType() == typeof(Group)) 
    { 
        dateX = DateTime.ParseExact(((Group)x).Key.ToString(), "dd.mm.yyyy", CultureInfo.InvariantCulture); 
        dateY = DateTime.ParseExact(((Group)y).Key.ToString(), "dd.mm.yyyy", CultureInfo.InvariantCulture); 
    } 
    else 
    { 
        dateX = (DateTime)x; 
        dateY = (DateTime)y; 
    } 
 
    if (DateTime.Compare(dateX, dateY) == 0) 
        return 0; 
 
    if (DateTime.Compare(dateX, dateY) >= 0) 
        return SortDirection == Syncfusion.Data.ListSortDirection.Ascending ? 1 : -1; 
    else 
        return SortDirection == Syncfusion.Data.ListSortDirection.Ascending ? -1 : 1; 
} 
  
We have also attached the sample for your reference in the following link. 
 
We hope this helps, let us know if you need further assistance on this. 
 
Regards, 
Pradeep Kumar B 



AN Andy May 28, 2020 11:32 AM UTC

Thank you! It works now :)


PK Pradeep Kumar Balakrishnan Syncfusion Team May 29, 2020 12:53 AM UTC

Hi Maria, 
 
Thank you for the update. We are happy that the mentioned issue resolved at your end. 
Please let us know, if you need any further assistance. 
 
Regards, 
Pradeep Kumar B 


Loader.
Up arrow icon