Example #1
0
            public void SortBy()
            {
                if (!Sorted && (CategoryTable == null || CategoryTable.Sorted))
                {
                    var orderByProp = Type.GetProperties().SingleOrDefault(p => columnAttr(p) != null && columnAttr(p).OrderBy);
                    Func <object, object> getPropAccessor = null;
                    if (orderByProp != null)
                    {
                        getPropAccessor = TableInfo.BuildGetAccessor(orderByProp.GetGetMethod());
                    }
                    bool hasCategory = CategoryTable != null;

                    if (!hasCategory && orderByProp != null)
                    {
                        this.Sort((x, y) => (getPropAccessor(x) as IComparable).CompareTo(getPropAccessor(y) as IComparable));
                    }
                    else if (hasCategory)
                    {
                        List <IEntity>[] lists = Enumerable.Repeat((List <IEntity>)null, CategoryTable.Count)
                                                 .Select(x => new List <IEntity>())
                                                 .ToArray();
                        foreach (var e in this)
                        {
                            lists[e.Category.ID - 1].Add(e);
                        }

                        if (orderByProp != null)
                        {
                            foreach (var list in lists)
                            {
                                list.Sort((x, y) => (getPropAccessor(x) as IComparable).CompareTo(getPropAccessor(y) as IComparable));
                            }
                        }
                        if (!outer.sources.ContainsKey(CategoryTable.Type))
                        {
                            outer.sources.Add(CategoryTable.Type, lists);
                        }
                        else
                        {
                            int i = 0;
                            foreach (var list in outer.sources[CategoryTable.Type])
                            {
                                list.AddRange(lists[i++]);
                            }
                        }
                        Clear();
                        AddRange(lists.SelectMany(x => x));
                    }

                    Sorted = true;
                    complete();
                }
            }