using System;
using System.Collections;
namespace ArrayListSORTING
{
public enum SortDirection
{
Asc,
Desc
}
public class CategoryComparer : IComparer
{
private SortDirection m_direction = SortDirection.Asc;
public CategoryComparer() : base() { }
public CategoryComparer(SortDirection direction)
{
this.m_direction = direction;
}
int IComparer.Compare(object x, object y)
{
Category categoryX = (Category) x;
Category categoryY = (Category) y;
if (categoryX == null && categoryY == null)
{
return 0;
}
else if (categoryX == null && categoryY != null)
{
return (this.m_direction == SortDirection.Asc) ? -1 : 1;
}
else if (categoryX != null && categoryY == null)
{
return (this.m_direction == SortDirection.Asc) ? 1 : -1;
}
else
{
return (this.m_direction == SortDirection.Asc) ?
categoryX.TypeCode.CompareTo(categoryY.TypeCode) :
categoryY.TypeCode.CompareTo(categoryX.TypeCode);
}
}
}
}

Join the conversation! Your thoughts help the community grow.