private void DoSort(OLVColumn columnToSort, SortOrder order)
{
// Sanity checks
if (this.GetItemCount() == 0 || this.Columns.Count == 0)
return;
// Fill in default values, if the parameters don't make sense
if (this.ShowGroups) {
columnToSort = columnToSort ?? this.GetColumn(0);
if (order == SortOrder.None) {
order = this.Sorting;
if (order == SortOrder.None)
order = SortOrder.Ascending;
}
}
// Give the world a chance to fiddle with or completely avoid the sorting process
BeforeSortingEventArgs args = this.BuildBeforeSortingEventArgs(columnToSort, order);
this.OnBeforeSorting(args);
if (args.Canceled)
return;
// Virtual lists don't preserve selection, so we have to do it specifically
// THINK: Do we need to preserve focus too?
IList selection = this.VirtualMode ? this.SelectedObjects : null;
this.ClearHotItem();
// Finally, do the work of sorting, unless an event handler has already done the sorting for us
if (!args.Handled) {
// Sanity checks
if (args.ColumnToSort != null && args.SortOrder != SortOrder.None) {
if (this.ShowGroups)
this.BuildGroups(args.ColumnToGroupBy, args.GroupByOrder, args.ColumnToSort, args.SortOrder,
args.SecondaryColumnToSort, args.SecondarySortOrder);
else if (this.CustomSorter != null)
this.CustomSorter(args.ColumnToSort, args.SortOrder);
else
this.ListViewItemSorter = new ColumnComparer(args.ColumnToSort, args.SortOrder,
args.SecondaryColumnToSort, args.SecondarySortOrder);
}
}
if (this.ShowSortIndicators)
this.ShowSortIndicator(args.ColumnToSort, args.SortOrder);
this.LastSortColumn = args.ColumnToSort;
this.LastSortOrder = args.SortOrder;
if (selection != null && selection.Count > 0)
this.SelectedObjects = selection;
this.RefreshHotItem();
this.OnAfterSorting(new AfterSortingEventArgs(args));
}