国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

c#集合快速排序類實現代碼分享

2020-01-24 03:00:00
字體:
來源:轉載
供稿:網友

說明:

1、集合類型參數化;

2、可根據集合中的對象的各個屬性進行排序,傳入屬性名稱即可;

注:屬性必須實現了IComparable接口,C#中int、datetime、string等基本類型都已經實現了IComparable接口。

復制代碼 代碼如下:

/// <summary>
    /// 對集合進行排序,如
    /// List<User> users=new List<User>(){.......}
    /// ListSorter.SortList<list<User>,User>(ref users,"Name",SortDirection.Ascending);
    /// </summary>
    public class ListSorter
    {
        public static void SortList<TCollection, TItem>(ref TCollection list, string property, SortDirection direction) where TCollection : IList<TItem>
        {
            PropertyInfo[] propertyinfos = typeof(TItem).GetProperties();
            foreach (PropertyInfo propertyinfo in propertyinfos)
            {
                if (propertyinfo.Name == property)          //取得指定的排序屬性
             // http://www.cnblogs.com/sosoft/
                {
                    QuickSort<TCollection, TItem>(ref list, 0, list.Count - 1, propertyinfo, direction);
                }
            }
        }
        /// <summary>
        /// 快速排序算法
        /// </summary>
        /// <typeparam name="TCollection">集合類型,需要實現Ilist<T>集合</typeparam>
        /// <typeparam name="TItem">集合中對象的類型</typeparam>
        /// <param name="list">集合對象</param>
        /// <param name="left">起始位置,從0開始</param>
        /// <param name="right">終止位置</param>
        /// <param name="propertyinfo">集合中對象的屬性,屬性必須要實現IComparable接口</param>
        /// <param name="direction">排序類型(升序或降序)</param>
        private static void QuickSort<TCollection, TItem>(ref TCollection list, int left, int right, PropertyInfo propertyinfo, SortDirection direction) where TCollection : IList<TItem>
        {
            if (left < right)
            {
                int i = left, j = right;
                TItem key = list[left];
                while (i < j)
                {
                    if (direction == SortDirection.Ascending)
                    {
                        while (i < j && ((IComparable)propertyinfo.GetValue(key, null)).CompareTo((IComparable)propertyinfo.GetValue(list[j], null)) < 0)
                        {
                            j--;
                        }
                        if (i < j)
                        {
                            list[i] = list[j];
                            i++;
                        }

                        while (i < j && ((IComparable)propertyinfo.GetValue(key, null)).CompareTo((IComparable)propertyinfo.GetValue(list[i], null)) > 0)
                        {
                            i++;
                        }
                        if (i < j)
                        {
                            list[j] = list[i];
                            j--;
                        }
                        list[i] = key;
                    }
                    else
                    {
                        while (i < j && ((IComparable)propertyinfo.GetValue(key, null)).CompareTo((IComparable)propertyinfo.GetValue(list[j], null)) > 0)
                        {
                            j--;
                        }
                        if (i < j)
                        {
                            list[i] = list[j];
                            i++;
                        }

                        while (i < j && ((IComparable)propertyinfo.GetValue(key, null)).CompareTo((IComparable)propertyinfo.GetValue(list[i], null)) < 0)
                        {
                            i++;
                        }
                        if (i < j)
                        {
                            list[j] = list[i];
                            j--;
                        }
                        list[i] = key;
                    }
                }
                //執行遞歸調用
                QuickSort<TCollection, TItem>(ref list, left, i - 1, propertyinfo, direction);
                QuickSort<TCollection, TItem>(ref list, i + 1, right, propertyinfo, direction);
            }
        }
    }
    /// <summary>
    /// 排序類型
    /// </summary>
    public enum SortDirection
    {
        Ascending,
        Descending
    }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 女性| 舟山市| 民乐县| 新平| 南漳县| 依安县| 枣阳市| 杂多县| 沂源县| 吕梁市| 甘洛县| 伊宁县| 岳普湖县| 易门县| 武山县| 察哈| 武邑县| 昌都县| 长丰县| 嵩明县| 敖汉旗| 荣成市| 张掖市| 会宁县| 高碑店市| 珲春市| 武宁县| 绥宁县| 化州市| 万宁市| 桃源县| 安塞县| 托克逊县| 普安县| 宣武区| 石棉县| 临潭县| 门源| 南城县| 渝北区| 绿春县|