public static T[] SortByStrategy <T>(this T[] list, ICompareStrategy <T> compare)
        {
            if (list == null || compare == null)
            {
                throw new ArgumentException("list can't be null");
            }

            for (int i = 1; i < list.Count(); i++)
            {
                for (int j = i; j < list.Count(); j++)
                {
                    //自訂一個規則介面,比較方式由外部提供 由外部注入!!
                    if (compare.Compare(list[i - 1], list[j]) > 0)
                    {
                        Swap(list, j, i);
                    }
                }
            }

            return(list);
        }
Пример #2
0
        public static IEnumerable <T> SortBy <T>(this T[] list, ICompareStrategy <T> compare)
        {
            if (list == null || compare == null)
            {
                throw new ArgumentException("list can't be null");
            }

            for (int i = 1; i < list.Count(); i++)
            {
                for (int j = i; j < list.Count(); j++)
                {
                    if (compare.Compare(list[i - 1], list[j]) > 0)
                    {
                        T temp = list[j];
                        list[j]     = list[i - 1];
                        list[i - 1] = temp;
                    }
                }
            }

            return(list);
        }