XPTable.Models.Table.Sort C# (CSharp) Method

Sort() private method

Sorts the specified column in the specified sort direction
private Sort ( int index, Column column, SortOrder sortOrder, bool stable ) : void
index int The index of the column to sort
column Column The column to sort
sortOrder SortOrder The direction the column is to be sorted
stable bool Specifies whether a stable sorting method /// should be used to sort the column
return void
        private void Sort(int index, Column column, SortOrder sortOrder, bool stable)
        {
            if (this.TableModel == null)
            {
                return;
            }

            // make sure a null comparer type doesn't sneak past
            ComparerBase comparer = null;

            if (column.Comparer != null)
            {
                comparer = (ComparerBase)Activator.CreateInstance(column.Comparer, new object[] { this.TableModel, index, sortOrder });
            }
            else if (column.DefaultComparerType != null)
            {
                comparer = (ComparerBase)Activator.CreateInstance(column.DefaultComparerType, new object[] { this.TableModel, index, sortOrder });
            }
            else
            {
                return;
            }

            column.InternalSortOrder = sortOrder;

            // create the comparer
            SorterBase sorter = null;

            // work out which sort method to use.
            // - InsertionSort/MergeSort are stable sorts,
            //   whereas ShellSort/HeapSort are unstable
            // - InsertionSort/ShellSort are faster than
            //   MergeSort/HeapSort on small lists and slower
            //   on large lists
            // so we choose based on the size of the list and
            // whether the user wants a stable sort
            if (this.SortType == SortType.AutoSort)
            {
                if (this.TableModel.Rows.Count < 1000)
                {
                    if (this.StableSort)
                    {
                        sorter = new InsertionSorter(this.TableModel, index, comparer, sortOrder);
                    }
                    else
                    {
                        sorter = new ShellSorter(this.TableModel, index, comparer, sortOrder);
                    }
                }
                else
                {
                    if (this.StableSort)
                    {
                        sorter = new MergeSorter(this.TableModel, index, comparer, sortOrder);
                    }
                    else
                    {
                        sorter = new HeapSorter(this.TableModel, index, comparer, sortOrder);
                    }
                }
            }
            else
            {
                switch (this.SortType)
                {
                    case SortType.HeapSort:
                        sorter = new HeapSorter(this.TableModel, index, comparer, sortOrder);
                        break;
                    case SortType.InsertionSort:
                        sorter = new InsertionSorter(this.TableModel, index, comparer, sortOrder);
                        break;
                    case SortType.MergeSort:
                        sorter = new MergeSorter(this.TableModel, index, comparer, sortOrder);
                        break;
                    case SortType.ShellSort:
                        sorter = new ShellSorter(this.TableModel, index, comparer, sortOrder);
                        break;
                    default:
                        throw new ApplicationException("Invalid Sort Type - " + this.SortType.ToString());
                }
            }

            sorter.SecondarySortOrders = this.ColumnModel.SecondarySortOrders;
            sorter.SecondaryComparers = this.GetSecondaryComparers(this.ColumnModel.SecondarySortOrders);

            // don't let the table redraw
            this.BeginUpdate();

            this.OnBeginSort(new ColumnEventArgs(column, index, ColumnEventType.Sorting, null));

            // Added by -= Micronn =- on 22 dec 2005
            Row focusedRow = null;

            if (this.FocusedCell != CellPos.Empty)
                focusedRow = this.tableModel.Rows[this.FocusedCell.Row];

            sorter.Sort();

            // Added by -= Micronn =- on 22 dec 2005
            if (focusedRow != null)
            {
                this.FocusedCell = new CellPos(focusedRow.Index, this.FocusedCell.Column);
                this.EnsureVisible(this.FocusedCell);
            }

            this.OnEndSort(new ColumnEventArgs(column, index, ColumnEventType.Sorting, null));

            // redraw any changes
            this.EndUpdate();
        }

Same methods

Table::Sort ( ) : void
Table::Sort ( bool stable ) : void
Table::Sort ( int column ) : void
Table::Sort ( int column, SortOrder sortOrder ) : void
Table::Sort ( int column, SortOrder sortOrder, bool stable ) : void
Table::Sort ( int column, bool stable ) : void
Table