最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

c#集合快速排序類實(shí)現(xiàn)代碼分享

 更新時(shí)間:2013年12月13日 10:45:51   作者:  
這篇文章主要介紹了C#實(shí)現(xiàn)集合排序類,大家參考使用吧

說(shuō)明:

1、集合類型參數(shù)化;

2、可根據(jù)集合中的對(duì)象的各個(gè)屬性進(jìn)行排序,傳入屬性名稱即可;

注:屬性必須實(shí)現(xiàn)了IComparable接口,C#中int、datetime、string等基本類型都已經(jīng)實(shí)現(xiàn)了IComparable接口。

復(fù)制代碼 代碼如下:

/// <summary>
    /// 對(duì)集合進(jìn)行排序,如
    /// 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)          //取得指定的排序?qū)傩?BR>             // http://www.cnblogs.com/sosoft/
                {
                    QuickSort<TCollection, TItem>(ref list, 0, list.Count - 1, propertyinfo, direction);
                }
            }
        }
        /// <summary>
        /// 快速排序算法
        /// </summary>
        /// <typeparam name="TCollection">集合類型,需要實(shí)現(xiàn)Ilist<T>集合</typeparam>
        /// <typeparam name="TItem">集合中對(duì)象的類型</typeparam>
        /// <param name="list">集合對(duì)象</param>
        /// <param name="left">起始位置,從0開始</param>
        /// <param name="right">終止位置</param>
        /// <param name="propertyinfo">集合中對(duì)象的屬性,屬性必須要實(shí)現(xiàn)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;
                    }
                }
                //執(zhí)行遞歸調(diào)用
                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
    }

相關(guān)文章

最新評(píng)論

金坛市| 霸州市| 万安县| 玉门市| 平塘县| 嘉禾县| 上林县| 平顺县| 京山县| 嵩明县| 屯门区| 花垣县| 辰溪县| 永兴县| 克山县| 横山县| 达州市| 永修县| 雷山县| 大邑县| 太湖县| 繁峙县| 启东市| 定边县| 泉州市| 上杭县| 阆中市| 房山区| 延津县| 山东省| 广东省| 东乌珠穆沁旗| 明星| 双柏县| 新昌县| 高碑店市| 锡林浩特市| 琼中| 长岭县| 略阳县| 县级市|