/// <summary> /// 构造函数 /// </summary> /// <param name="needtoexcute">把已经转化成数组形式的数据源传入执行条件筛选形成key数组</param> /// <param name="keyselector">条件筛选方法</param> /// <param name="next">比较器的下一个引用</param> /// <param name="comparer">这个sorter的比较器</param> public MyOrderThenBySorter(TMySource[] needtoexcute, 内置委托.Func <TMySource, TMyKey> keyselector, MyOrderThenBySorter <TMySource, TMyKey> next, 系统内置接口.IComparer <TMyKey> comparer) { this.needtoexcute = needtoexcute; this.keyselector = keyselector; this.next = next; this.comparer = comparer; }
public MyOrderThenBySorter <TSource, TKey> GetSorter(TSource[] local, MyOrderThenBySorter <TSource, TKey> next) { MyOrderThenBySorter <TSource, TKey> sorter = new MyOrderThenBySorter <TSource, TKey>(local, this.keyselector, next, this.comparer); if (this.parent != null) { sorter = this.parent.GetSorter(local, sorter); } return(sorter); }
//为外界提供一个获取这个已经排序好的序列的方法 public 数据结构.IEnumerable <TSource> GetEveryElement() { #region 假设以后再来实现吧 //把本地的数据源转化成数组的形式,再调用数组的快排的方法(或者在sorthelper中定义一个新的快排的方法 as you like) //假设1:对序列的每一个元素执行Keyselector的方法,并且把它保存到一个Key的数组中,只有当外界对这个序列进行枚举的时候这个Key数组才会被初始化 //假设2:对数据源中的每一个元素执行keyselector的方法,如果这个序列是需要进行多条件排序那么必须把每一次的排序的条件保存起来 //保存的keyselector的数组是按照倒序的条件进行排序的 //MyList<TSource> templist = this.source.ToList(); //_keyarray = new TKey[templist.Count]; //for (int i = 0; i < templist.Count; i++) //{ // _keyarray[i] = this.keyselector(templist[i]); //} ////用来保存Key数组的下标值 //int[] map = new int[templist.Count]; //for (int i = 0; i < templist.Count; i++) //{ // map[i] = i; //} #endregion //把本地的数据源转化成数组的形式或者调用转化成List的形式 ChangeSource <TSource> local = new ChangeSource <TSource>(); local.SourceToArray(this.source); MyOrderThenBySorter <TSource, TKey> sorter = GetSorter(local._temp, null); int[] final = sorter.Sort(); //根据final的数组的下标把元素遍历出来,实现最终的排序 //[8,3,4,5,6,7,2] MyList <TSource> list = new MyList <TSource>(); for (int i = 0; i < final.Length; i++) { list.Add(local._temp[final[i]]); } return(list); }