Thursday, August 30, 2007

Business layer sorting

Sometime, you need to sort a list of business object, for example you have a list of SLPolicy object in an arraylist, and you want to sort it in different ways, like by policynumber, expiration date, or agent code. You want to do this as easy as this:
ArrayList Policies = CreateListofPolicies() ;
SLPolicy.SortBy = "PolicyNumber";
Policies.Sort();
if that's what you want, you need to implement some class like below:
    

    [Serializable]
public class SLPolicy : IComparable
{

#region IComparable Members

public int CompareTo(object obj)
{
switch (_container.Container.SortKind)
{
case SLSortableKind.AgentCodeAsc:
return _agentCode.CompareTo(((SLPolicy) obj).AgentCode);

case SLSortableKind.AgentCodeDesc:
return ((SLPolicy) obj).AgentCode.CompareTo(_agentCode);

case SLSortableKind.InsuredNameAsc:
case SLSortableKind.InsuredNameDesc:
case SLSortableKind.ExpDateAsc:
return _expDate.CompareTo(((SLPolicy) obj).ExpDate);

case SLSortableKind.ExpDateDesc:
return ((SLPolicy) obj).ExpDate.CompareTo(_expDate);

default:
return 0;
}
}

#endregion

}

No comments: