Why ArgumentException occurs when sort the collection more than 16

  • Thread starter Thread starter Mohanmj
  • Start date Start date
M

Mohanmj

Guest
I have tried to sort my RageInfo using custom comparer it is working fine for 16 ranges. If I use more than 16 ranges, it will be throwing following exception,

Exception details

System.ArgumentException was unhandled

HResult=-2147024809

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: 'ConsoleApplication1.ColComparer'.

Source=mscorlib

StackTrace:

at System.Collections.Generic.IntrospectiveSortUtilities.ThrowOrIgnoreBadComparer(Object comparer)

at System.Array.SorterObjectArray.IntrospectiveSort(Int32 left, Int32 length)

at System.Array.Sort(Array keys, Array items, Int32 index, Int32 length, IComparer comparer)

at System.Array.Sort(Array array, IComparer comparer)

at ConsoleApplication1.Program.SortRanges(Int32 count) in D:\Testing\Array\ConsoleApplication1\ConsoleApplication1\Program.cs:line 28

at ConsoleApplication1.Program.Main(String[] args) in D:\Testing\Array\ConsoleApplication1\ConsoleApplication1\Program.cs:line 15

at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)

at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)

at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()

at System.Threading.ThreadHelper.ThreadStart_Context(Object state)

at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)

at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)

at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)

at System.Threading.ThreadHelper.ThreadStart()

InnerException:

Code example

SortRanges(16);//Working fine
SortRanges(17);//Exceptiom has thrown

public static void SortRanges(int count)
{
List<RangeInfo> range = new List<RangeInfo>();
for (int i = 0; i < count; i++)
{
range.Add(new RangeInfo(2 + (i * 2), RangeInfoType.Rows));
}
RangeInfo[] ranges = new RangeInfo[count];
range.CopyTo(ranges, 0);
Array.Sort(ranges, new ColComparer());
}

internal class ColComparer : IComparer
{
public int Compare(object a, object b)
{
if (a == null || b == null)
{
return System.Collections.Comparer.Default.Compare(a, b);
}

RangeInfo r0 = (RangeInfo)a;
RangeInfo r1 = (RangeInfo)b;

if (r0 == r1)
{
return 0;
}
else
{
if (r0.RangeType == RangeInfoType.Table)
{
return -1;
}
else if (r1.RangeType == RangeInfoType.Table)
{
return 1;
}
else if (r0.RangeType == RangeInfoType.Rows)
{
return -1;
}
else if (r1.RangeType == RangeInfoType.Rows)
{
return 1;
}
else
{
return 0;
}
}
}
}



Sample link: ConsoleApplication1.zip

Thanks,





Mohanraj G

Continue reading...
 
Back
Top