zoukankan      html  css  js  c++  java
  • 【转】对实体类进行排序

    代码
    /// <summary>
            
    /// 对实体类进行排序
            
    /// </summary>
            
    /// <typeparam name="T">实体类型,如:User</typeparam>
            
    /// <param name="list">实体类的数组</param>
            
    /// <param name="order">排序字段(必须为属性)</param>
            
    /// <param name="asc">是否按正序排序</param>
            public static void Sort<T>(object[] list, string order, bool asc)
            {
                Type type 
    = typeof(T);
                PropertyInfo[] pros 
    = type.GetProperties();
                PropertyInfo pro 
    = pros[0];
                order 
    = order.ToLower();
                
    for (int i = 0; i < pros.Length; i++)
                {
                    
    if (pros[i].Name.ToLower().Equals(order))
                    {
                        pro 
    = pros[i];
                        
    break;
                    }
                }
                
    object obj;
                
    int j, k = 1;
                
    bool done = false;
                
    int len = list.Length;
                
    while (k < len && !done)
                {
                    done 
    = true;
                    
    for (j = 0; j < len - k; j++)
                    {
                        
    int b = pro.GetValue(list[j], null).ToString().CompareTo(pro.GetValue(list[j + 1], null).ToString());
                        
    if ((asc && b > 0|| (!asc && b < 0))
                        {
                            done 
    = false;
                            obj 
    = list[j];
                            list[j] 
    = list[j + 1];
                            list[j 
    + 1= obj;
                        }
                    }
                    k
    ++;
                }
            }
  • 相关阅读:
    OE_ORDER_PUB.PROCESS_ORDER to Apply hold on a sales order
    父母
    更新价目表价格
    Netbeans6.8 主题美化及去掉红线
    IIS宿主wcf时的错误解决
    ubuntu安装deb,rpm安装包方法
    高效程序员的45个习惯
    数据库无法保存中文的解决
    1366*768 play game full screen
    项目管理部分随笔索引
  • 原文地址:https://www.cnblogs.com/gebenhagen/p/1735451.html
Copyright © 2011-2022 走看看