尝试执行列表排序时,出现以下错误:
无法排序,因为 IComparer.Compare() 方法返回的结果不一致。某个值与其自身比较不相等,或者一个值与另一个值重复比较会产生不同的结果。
请提出此问题背后可能的原因。
堆栈跟踪:
消息:无法排序,因为 IComparer.Compare() 方法返回不一致的结果。某个值与其自身比较不相等,或者一个值与另一个值重复比较会产生不同的结果。 IComparer:“Premise.PremiseLIVE.CommGateway.Workflows.Sequential.SequentialWorkflowData.TransportJobPriorityDataSort”。 StackTrace:位于 System.Collections.Generic.IntrospectiveSortUtilities.ThrowOrIgnoreBadComparer(对象比较器) 在 System.Collections.Generic.ArraySortHelper
1.Sort(T[] keys, Int32 index, Int32 length, IComparer
1 比较器)
在 System.Array.Sort[T](T[] 数组,Int32 索引,Int32 长度,IComparer1 comparer) at System.Collections.Generic.List
1.Sort(Int32 索引,Int32 计数,IComparer`1 比较器)
在 Premise.PremiseLIVE.CommGateway.Workflows.Sequential.WorkflowActivities.wcSortTransportJobs.ExecuteSequential(ActivityExecutionContextexecutionContext)
来源:mscorlib
TargetSite:无效 ThrowOrIgnoreBadComparer(System.Object)”
代码:
IComparer<JobPriorityData> comparer = null;
comparer = new TransportJobPriorityDataSort( orgTransportTyps, transportPriorities, false );
if ( comparer != null )
{
jobList.Sort( comparer );
}
public TransportJobPriorityDataSort( OrgTransportTypCollection orgTransportTyps, TransportPriorityCollection transportPriorities, bool ascending )
{
_orgTransportTyps = orgTransportTyps;
_transportPriorities = transportPriorities;
_ascending = ascending;
if ( !_ascending )
{
_xLTy = 1;
_xGTy = -1;
}
_currentTime = DatabaseTimeHelper.DatabaseUtcTime;
}
public int Compare( JobPriorityData x, JobPriorityData y )
{
try
{
// past-due jobs prioritized over not-past-due.
if ( ( x.TransportPickupTime.Value <= _currentTime ) && ( y.TransportPickupTime.Value > _currentTime ) )
{
return ( _xGTy );
}
if ( ( x.TransportPickupTime.Value > _currentTime ) && ( y.TransportPickupTime.Value <= _currentTime ) )
{
return ( _xLTy );
}
// either: (A) both jobs are past-due or (B) neither job is past-due.
// Order by Typ
if ( _orgTransportTyps[ x.JobTransportType ].AssignSeqNbr > _orgTransportTyps[ y.JobTransportType ].AssignSeqNbr )
{
return ( _xLTy );
}
if ( _orgTransportTyps[ x.JobTransportType ].AssignSeqNbr < _orgTransportTyps[ y.JobTransportType ].AssignSeqNbr )
{
return ( _xGTy );
}
// then by pickup time
if ( x.TransportPickupTime < y.TransportPickupTime )
{
return ( _xGTy );
}
if ( x.TransportPickupTime < y.TransportPickupTime )
{
return ( _xLTy );
}
}
catch ( Exception )
{
}
return ( _xEQy );
}