예제 #1
0
파일: Table.cs 프로젝트: nithinphilips/SMOz
		/// <summary>
		/// Sorts the specified column in the specified sort direction
		/// </summary>
		/// <param name="index">The index of the column to sort</param>
		/// <param name="column">The column to sort</param>
		/// <param name="sortOrder">The direction the column is to be sorted</param>
		/// <param name="stable">Specifies whether a stable sorting method 
		/// should be used to sort the column</param>
		private void Sort(int index, Column column, SortOrder sortOrder, bool stable)
		{
			// 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.TableModel.Rows.Count < 1000)
			{
				if (stable)
				{
					sorter = new InsertionSorter(this.TableModel, index, comparer, sortOrder);
				}
				else
				{
					sorter = new ShellSorter(this.TableModel, index, comparer, sortOrder);
				}
			}
			else
			{
				if (stable)
				{
					sorter = new MergeSorter(this.TableModel, index, comparer, sortOrder);
				}
				else
				{
					sorter = new HeapSorter(this.TableModel, index, comparer, sortOrder);
				}
			}

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

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

			sorter.Sort();

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

			// redraw any changes
			this.EndUpdate();
		}
예제 #2
0
파일: Table.cs 프로젝트: antiduh/XPTable
        /// <summary>
        /// Sorts the specified column in the specified sort direction
        /// </summary>
        /// <param name="index">The index of the column to sort</param>
        /// <param name="column">The column to sort</param>
        /// <param name="sortOrder">The direction the column is to be sorted</param>
        /// <param name="stable">Specifies whether a stable sorting method 
        /// should be used to sort the column</param>
        private void Sort(int index, Column column, SortOrder sortOrder, bool stable)
        {
            // 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();
        }