示例#1
0
        private void addItem(object item)
        {
            if (GroupAdapter == null)
            {
                return;
            }

            IGroupHeader correctHeader = GroupAdapter.GenerateHeader(item);

            for (int i = 0; i < _collection.Count; i++)
            {
                if (_collection[i] is IGroupHeader)
                {
                    IGroupHeader header = _collection[i] as IGroupHeader;

                    int comp = correctHeader.CompareTo(header);

                    //if goes inside this header
                    if (comp == 0)
                    {
                        //binary search insert would have to walk to the end of the items anyways, so might as well do linear insert

                        //move to the first item
                        i++;

                        for (; i < _collection.Count; i++)
                        {
                            //if we're at the next header, insert at here
                            if (_collection[i] is IGroupFooter || _collection[i] is IGroupHeader)
                            {
                                _collection.Insert(i, item);
                                return;
                            }

                            //or if the item goes before the item in the list
                            else if (header.CompareInsideHeader(item, _collection[i]) < 0)
                            {
                                _collection.Insert(i, item);
                                return;
                            }

                            //otherwise keep looking through the list
                        }

                        //if it wasn't added, must be at the end of the list
                        _collection.Add(item);
                        return;
                    }

                    //if goes before this header
                    else if (comp < 0)
                    {
                        insert(i, correctHeader, item);
                        return;
                    }
                }
            }

            //item didn't find a matching header, needs a new header
            insert(_collection.Count, correctHeader, item);
            return;
        }