Пример #1
0
        /// <summary>
        /// Sorts the data rows of a DataColumnCollection (more accurate: of all columns belonging to a column group, see below), using a specified column.
        /// </summary>
        /// <param name="table">The DataColumnCollection where the data columns should be sorted.</param>
        /// <param name="cols">The columns which are used for determining the order of the entries. The sorting will be done by cols[0], then cols[1] and so on.
        /// ll this columns has to belong to the table and need to have the same column group number (otherwise an exception will be thrown). All columns with the same group number than this columns will be included in the sort process.</param>
        /// <param name="inAscendingOrder">If true, the table is sorted in ascending order. Otherwise, the table is sorted in descending order.</param>
        public static void SortRows(this DataColumnCollection table, DataColumn[] cols, bool inAscendingOrder)
        {
            if (cols == null || cols.Length == 0)
            {
                throw new ArgumentException("cols is null or empty");
            }

            using (var token = table.SuspendGetToken())
            {
                int groupNumber = table.GetColumnGroup(cols[0]);
                for (int i = 1; i < cols.Length; i++)
                {
                    if (groupNumber != table.GetColumnGroup(cols[i]))
                    {
                        throw new ArgumentException(string.Format("cols[{0}] has a deviating group number from cols[0]. Only columns belonging to the same group can be sorted", i));
                    }
                }

                for (int k = 0; k < cols.Length; k++)
                {
                    if (!table.ContainsColumn(cols[k]))
                    {
                        throw new ArgumentException("The sorting columnd provided must all be part of the DataColumnCollection (otherwise the swap algorithm can not sort this column)");
                    }
                }

                HeapSort(cols[0].Count, new MultipleDataColumnComparer(cols, inAscendingOrder).Compare, new DataColumnCollectionRowSwapper(table, groupNumber).Swap);
            }
        }
Пример #2
0
        /// <summary>
        /// Sorts the data rows of a DataColumnCollection (more accurate: of all columns belonging to a column group, see below), using a specified column.
        /// </summary>
        /// <param name="table">The DataColumnCollection where the data columns should be sorted.</param>
        /// <param name="col">The column which is used for determining the order of the entries.
        /// This column has to belong to the table (otherwise an exception will be thrown). All columns with the same group number than this column will be included in the sort process.</param>
        /// <param name="inAscendingOrder">If true, the table is sorted in ascending order. Otherwise, the table is sorted in descending order.</param>
        public static void SortRows(this DataColumnCollection table, DataColumn col, bool inAscendingOrder)
        {
            if (!table.ContainsColumn(col))
            {
                throw new ArgumentException("The sorting column provided must be part of the DataColumnCollection (otherwise the swap algorithm can not sort this column)");
            }

            using (var token = table.SuspendGetToken())
            {
                int columnGroup = table.GetColumnGroup(col);
                if (col is DoubleColumn)
                {
                    HeapSort(col.Count, new DoubleColumnComparer((DoubleColumn)col, inAscendingOrder).Compare, new DataColumnCollectionRowSwapper(table, columnGroup).Swap);
                }
                else
                {
                    HeapSort(col.Count, new DataColumnComparer(col, inAscendingOrder).Compare, new DataColumnCollectionRowSwapper(table, columnGroup).Swap);
                }
            }
        }