I'm looking for a better way to implement a huge CompareTo function that relies on an Enum to perform sorting. The data structure is essentially a flattened Multi Level Group. Any ideas and concept to make this function easier to understand and extend are appreciated.
With the exception of of the Field 1,2,3 names everything is exactly as in my code.
public int CompareTo(object obj)
{
var y = (ListEntry)obj;
var result = string.CompareOrdinal(Field1, y.Field1);
if (result != 0) { return result; }
switch (Typ)
{
case TypEnum.None:
switch (y.Typ)
{
case TypEnum.None:
return string.CompareOrdinal(Field2, y.Field2);
case TypEnum.MessageField2Sum:
case TypEnum.MessageField2:
case TypEnum.MessageField2Caption:
case TypEnum.Field1:
return -1;
case TypEnum.Field3:
case TypEnum.Field2Group:
case TypEnum.SubGroup:
result = string.CompareOrdinal(Field3, y.Field3);
if (result != 0)
{
return result;
}
return -1;
case TypEnum.Field1Caption:
return 1;
default:
throw new ArgumentOutOfRangeException();
}
case TypEnum.Field1Caption:
return -1;
case TypEnum.Field1:
return 1;
case TypEnum.Field3:
switch (y.Typ)
{
case TypEnum.None:
case TypEnum.Field2Group:
case TypEnum.Field3:
case TypEnum.Field1Caption:
result = string.CompareOrdinal(Field3, y.Field3);
if (result != 0)
{
return result;
}
Debug.Assert(y.Typ != TypEnum.Field3);
return 1;
case TypEnum.SubGroup:
result = string.CompareOrdinal(Field3, y.Field3);
if (result != 0)
{
return result;
}
Debug.Assert(y.Typ != TypEnum.Field3);
return -1;
case TypEnum.Field1:
case TypEnum.MessageField2Sum:
case TypEnum.MessageField2:
case TypEnum.MessageField2Caption:
return -1;
default:
throw new ArgumentOutOfRangeException();
}
case TypEnum.Field2Group:
switch (y.Typ)
{
case TypEnum.None:
case TypEnum.Field2Group:
case TypEnum.Field1Caption:
result = string.CompareOrdinal(Field3, y.Field3);
if (result != 0)
{
return result;
}
Debug.Assert(y.Typ != TypEnum.Field2Group);
return 1;
case TypEnum.Field3:
case TypEnum.SubGroup:
result = string.CompareOrdinal(Field3, y.Field3);
if (result != 0)
{
return result;
}
return -1;
case TypEnum.MessageField2Sum:
case TypEnum.MessageField2Caption:
case TypEnum.MessageField2:
case TypEnum.Field1:
return -1;
default:
throw new ArgumentOutOfRangeException();
}
case TypEnum.SubGroup:
switch (y.Typ)
{
case TypEnum.None:
case TypEnum.Field2Group:
case TypEnum.Field3:
case TypEnum.Field1Caption:
case TypEnum.SubGroup:
result = string.CompareOrdinal(Field3, y.Field3);
if (result != 0)
{
return result;
}
Debug.Assert(y.Typ != TypEnum.SubGroup);
return 1;
case TypEnum.MessageField2Sum:
case TypEnum.MessageField2:
case TypEnum.MessageField2Caption:
case TypEnum.Field1:
return -1;
default:
throw new ArgumentOutOfRangeException();
}
case TypEnum.MessageField2Caption:
switch (y.Typ)
{
case TypEnum.None:
case TypEnum.Field2Group:
case TypEnum.Field3:
case TypEnum.Field1Caption:
case TypEnum.SubGroup:
return 1;
case TypEnum.MessageField2:
case TypEnum.MessageField2Sum:
case TypEnum.Field1:
return -1;
case TypEnum.MessageField2Caption:
default:
throw new ArgumentOutOfRangeException();
}
case TypEnum.MessageField2Sum:
switch (y.Typ)
{
case TypEnum.None:
case TypEnum.Field2Group:
case TypEnum.Field3:
case TypEnum.Field1Caption:
case TypEnum.SubGroup:
case TypEnum.MessageField2Caption:
return 1;
case TypEnum.MessageField2:
result = string.CompareOrdinal(Field2, GetField2Group(y.Field2));
if (result != 0)
{
return result;
}
return 1;
case TypEnum.MessageField2Sum:
result = string.CompareOrdinal(Field2, y.Field2);
if (result != 0)
{
return result;
}
throw new ArgumentOutOfRangeException();
case TypEnum.Field1:
return -1;
default:
throw new ArgumentOutOfRangeException();
}
case TypEnum.MessageField2:
switch (y.Typ)
{
case TypEnum.None:
case TypEnum.Field2Group:
case TypEnum.Field3:
case TypEnum.Field1Caption:
case TypEnum.SubGroup:
case TypEnum.MessageField2Caption:
return 1;
case TypEnum.MessageField2:
result = string.CompareOrdinal(GetField2Group(Field2), GetField2Group(y.Field2));
if (result != 0)
{
return result;
}
result = string.CompareOrdinal(Field2, y.Field2);
if (result != 0)
{
return result;
}
throw new ArgumentOutOfRangeException();
case TypEnum.MessageField2Sum:
result = string.CompareOrdinal(GetField2Group(Field2), y.Field2);
if (result != 0)
{
return result;
}
return -1;
case TypEnum.Field1:
return -1;
default:
throw new ArgumentOutOfRangeException();
}
default:
throw new ArgumentOutOfRangeException();
}
throw new NotImplementedException();
}
MyObject
,MyEnum
,Field1
, etc. look like made-up identifiers. Please post real code — or at least code that realistically captures your intent — so that we can advise you properly. \$\endgroup\$