ColinOL

A Small Website

用户工具

站点工具


wiki:csharp:base-dsjjg-jxpx

对数据结构排序

#region 对数据结构排序
/// <summary>
/// 对数据结构排序
/// </summary>
/// <param name="list">数据列表</param>
/// <param name="order">升序排列</param> 
/// <param name="strProperty">排序字段</param>
/// <param name="_isFigure">字段为数字类型</param>
/// <returns></returns>
private List<IPlaneShow> SortIPlaneShows(IPlaneShow[] list, bool order, string strProperty, bool _isFigure)
{
    curSortProperty = strProperty;
    IsOrderly = order;
    IsFigure = _isFigure;
    Comparison<IPlaneShow> com = new Comparison<IPlaneShow>(Compare);
    List<IPlaneShow> listOri = new List<IPlaneShow>();
    listOri.AddRange(list);
    listOri.Sort(com); 
    return listOri; 
}
#region 排序辅助
//排序字段
string curSortProperty = "";
//顺序排序
bool IsOrderly = true;
//是否为数字类型的字段
bool IsFigure = false;
//排序器
int Compare(IPlaneShow A, IPlaneShow B)
{
    int result;
    CaseInsensitiveComparer objCompare = new CaseInsensitiveComparer();
    string strA = A.GetType().GetProperty(curSortProperty).GetValue(A, null).ToString();
    string strB = B.GetType().GetProperty(curSortProperty).GetValue(B, null).ToString();
    if (IsFigure)
    {
        //数字排序
        double dA = double.Parse(strA);
        double dB = double.Parse(strB);
        result = objCompare.Compare(strA, strB);
    }
    else
    {
        //字符排序
        result = objCompare.Compare(strA, strB);
    } 
    //正反序
    if (IsOrderly)
        result *= -1;
    return result;
} 
#endregion
#endregion
wiki/csharp/base-dsjjg-jxpx.txt · 最后更改: 2018/02/03 20:42 (外部编辑)