// // Summary: // Sorts the list based on a System.ComponentModel.PropertyDescriptor and a // System.ComponentModel.ListSortDirection. // // Parameters: // property: // The System.ComponentModel.PropertyDescriptor to sort by. // // direction: // One of the System.ComponentModel.ListSortDirection values. // // Exceptions: // System.NotSupportedException: // System.ComponentModel.IBindingList.SupportsSorting is false. public virtual void ApplySort(PropertyDescriptor property, ListSortDirection direction) { //Flip the properties if the parameters are the same if ((m_SortProperty == property) && (m_SortDirection == direction)) { direction = (direction == ListSortDirection.Ascending) ? ListSortDirection.Descending : ListSortDirection.Ascending; } // Apply and set the sort, if items to sort PropertyComparerHelper <T> pc = new PropertyComparerHelper <T>(property, direction); if (SortStarted != null) { SortStarted(this, null); } try { Sort(pc); m_SortProperty = property; m_SortDirection = direction; } finally { if (SortFinished != null) { SortFinished(this, null); } } }
/// <summary> /// Sorts implemented type by specified property /// </summary> /// <param name="propertyName"></param> /// <param name="ascending"></param> public void Sort(string propertyName, bool ascending) { //Flip the properties if the parameters are the same if (_propertyName == propertyName && _ascending == ascending) { _ascending = !ascending; } else //Else, new properties are set with the new values { _propertyName = propertyName; _ascending = ascending; } PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T)); PropertyDescriptor propertyDesc = properties.Find(propertyName, true); // Apply and set the sort, if items to sort PropertyComparerHelper <T> pc = new PropertyComparerHelper <T>(propertyDesc, (_ascending) ? ListSortDirection.Ascending : ListSortDirection.Descending); Sort(pc); }