private IComparer GetSortComparer(ListSortDescriptionCollection sortDescriptions) { bool needSubstitution = false; if (_sortSubstitutions.Count > 0) { foreach (ListSortDescription sortDescription in sortDescriptions) { if (_sortSubstitutions.ContainsKey(sortDescription.PropertyDescriptor.Name)) { needSubstitution = true; break; } } if (needSubstitution) { ListSortDescription[] sorts = new ListSortDescription[sortDescriptions.Count]; sortDescriptions.CopyTo(sorts, 0); for (int i = 0; i < sorts.Length; i++) { if (_sortSubstitutions.ContainsKey(sorts[i].PropertyDescriptor.Name)) { sorts[i] = new ListSortDescription(((SortSubstitutionPair)_sortSubstitutions[sorts[i].PropertyDescriptor.Name]).Substitute, sorts[i].SortDirection); } } sortDescriptions = new ListSortDescriptionCollection(sorts); } } return(new SortListPropertyComparer(sortDescriptions)); }
/// <summary> /// Core sort function. All sorting function uses this method. /// If the ListSortDescriptionCollection are the same stored /// in the collection, the collection is sorted yet, /// so it returns immediately /// </summary> /// <param name="sorts">List of SortDescription</param> public void ApplySort(ListSortDescriptionCollection sorts) { if (sorts == null) { return; } bool bMatch = false; if (_IsSorted && _SortDescriptions != null && _SortDescriptions.Count == sorts.Count) { bMatch = true; for (int i = 0; i < sorts.Count; i++) { ListSortDescription ldsSource = sorts[i]; ListSortDescription ldsTarget = _SortDescriptions[i]; if (ldsSource.PropertyDescriptor != ldsTarget.PropertyDescriptor || ldsSource.SortDirection != ldsTarget.SortDirection) { bMatch = false; break; } } } // descriptors are the same of previous sort operation? just exit if (bMatch) { return; } _SortDescriptions = sorts; Sort(new DescriptorComparison(sorts)); // IComparer<T> _IsSorted = true; }
public void Ctor_PropertyDescriptor_ListSortDirection(PropertyDescriptor property, ListSortDirection direction) { var sortDescription = new ListSortDescription(property, direction); Assert.Same(property, sortDescription.PropertyDescriptor); Assert.Equal(direction, sortDescription.SortDirection); }
private ListSortDescriptionCollection ParseOrderBy(string orderBy) { if ((orderBy == null) || (orderBy.Length == 0)) { throw new ArgumentNullException("orderBy"); } string[] textArray = orderBy.Split(new char[] { ',' }); ListSortDescription[] sorts = new ListSortDescription[textArray.Length]; ListSortDirection ascending = ListSortDirection.Ascending; for (int i = 0; i < textArray.Length; i++) { ascending = ListSortDirection.Ascending; string text = textArray[i].Trim(); if (text.ToUpper().EndsWith(" DESC")) { ascending = ListSortDirection.Descending; text = text.Substring(0, text.ToUpper().LastIndexOf(" DESC")); } else if (text.ToUpper().EndsWith(" ASC")) { text = text.Substring(0, text.ToUpper().LastIndexOf(" ASC")); } text = text.Trim(); PropertyDescriptor property = this.m_PropertyDescriptors[text]; if (property == null) { throw new ArgumentException(string.Format("The property \"{0}\" is not a valid property.", text)); } sorts[i] = new ListSortDescription(property, ascending); } return(new ListSortDescriptionCollection(sorts)); }
void IBindingListView.ApplySort(ListSortDescriptionCollection sorts) { ListSortDescription[] sortArray = new ListSortDescription[sorts.Count + 1]; sorts.CopyTo(sortArray, 1); sortArray[0] = fixedSort; ApplySort(sortArray); }
/// <summary> /// Creates a <seealso cref="ListSortDescriptionCollection"/> from the sort descriptions in the collection. /// </summary> /// <returns> /// A <see cref="ListSortDescriptionCollection"/> representing the sort descriptions. /// </returns> public ListSortDescriptionCollection GetListSortDescriptions() { if (targetType == null) { throw new ArgumentNullException("targetType"); } var props = TypeDescriptor.GetProperties(targetType); var sorts = new ListSortDescription[descriptions.Count]; for (var i = 0; i < descriptions.Count; i++) { var desc = descriptions[i]; if (desc.PropertyDescriptor != null) { sorts[i] = new ListSortDescription(desc.PropertyDescriptor, desc.Direction); } else { sorts[i] = new ListSortDescription(props.Find(desc.PropertyName, true), desc.Direction); } } return(new ListSortDescriptionCollection(sorts)); }
public void Sort(IDictionary <string, bool> columns) { if (columns != null && columns.Count > 0) { Guid selectedId = default(Guid); if (SelectedRow != null && SelectedRow.Index > -1) { selectedId = (Guid)SelectedRow.Cells[QueueColumns.Id.Name].Value; } ListSortDescription[] sortArray = new ListSortDescription[columns.Count]; int columnIndex = 0; foreach (KeyValuePair <string, bool> column in columns) { ListSortDirection direction = (column.Value) ? ListSortDirection.Ascending : ListSortDirection.Descending; PropertyDescriptor property = TypeDescriptor.GetProperties(typeof(IMediaItem))[QueueColumns.ColumnsByPropertyName[column.Key].PropertyName]; sortArray[columnIndex] = new ListSortDescription(property, direction); columnIndex++; } ListSortDescriptionCollection sorts = new ListSortDescriptionCollection(sortArray); grid.Sort(sorts); DrawSortGlyphs(columns); } }
protected override void OnSortingRangeRows(SortRangeRowsEventArgs e) { base.OnSortingRangeRows(e); if (DataSource == null || DataSource.AllowSort == false) { return; } System.ComponentModel.PropertyDescriptor propertyCol = Columns[e.KeyColumn].PropertyColumn; if (propertyCol != null) { ListSortDirection direction; if (e.Ascending) { direction = ListSortDirection.Ascending; } else { direction = ListSortDirection.Descending; } ListSortDescription[] sortsArray = new ListSortDescription[1]; sortsArray[0] = new ListSortDescription(propertyCol, direction); DataSource.ApplySort(new ListSortDescriptionCollection(sortsArray)); } else { DataSource.ApplySort(null); } }
/// <summary> /// Multiple property compare method. /// </summary> /// <param name="x">The first object to compare.</param> /// <param name="y">The second object to compare.</param> /// <param name="index">Zero based index of SortDescriptions collection.</param> protected virtual int RecursiveCompareInternal(T x, T y, int index) { if (index >= SortDescriptions.Count) { return(0); // termination condition } /* Get property values */ ListSortDescription listSortDesc = SortDescriptions[index]; object xValue = listSortDesc.PropertyDescriptor.GetValue(x); object yValue = listSortDesc.PropertyDescriptor.GetValue(y); int result; /* Determine sort order */ if (listSortDesc.SortDirection == ListSortDirection.Ascending) { result = CompareAscending(xValue, yValue); } else { result = CompareDescending(xValue, yValue); } /* If the properties are equal, compare the next property */ if (result == 0) { return(RecursiveCompareInternal(x, y, ++index)); } return(result); }
protected override void ApplySortCore(PropertyDescriptor property, ListSortDirection direction) { _sortProperty = property; _sortDirection = direction; // Get list to sort List <T> items = Items as List <T>; // Apply and set the sort, if items to sort if (items != null) { ListSortDescription sort = new ListSortDescription(property, direction); ListSortDescription[] sortArr = new ListSortDescription[_sortDescriptions.Count + 1]; _sortDescriptions.CopyTo(sortArr, 1); sortArr[0] = sort; _sortDescriptions = new ListSortDescriptionCollection(sortArr); ApplySort(_sortDescriptions); _isSorted = true; } else { _isSorted = false; } // Let bound controls know they should refresh their views OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1)); }
// NOTE: Probably the parsing can be improved void ProcessSortString(string sort) { // Only keep simple whitespaces in the middle sort = Regex.Replace(sort, "( )+", " "); string [] properties = sort.Split(','); PropertyDescriptorCollection prop_descs = GetItemProperties(null); if (properties.Length == 1) { ListSortDescription sort_desc = GetListSortDescription(prop_descs, properties [0]); ApplySort(sort_desc.PropertyDescriptor, sort_desc.SortDirection); } else { if (!SupportsAdvancedSorting) { throw new ArgumentException("value"); } ListSortDescription [] sort_descs = new ListSortDescription [properties.Length]; for (int i = 0; i < properties.Length; i++) { sort_descs [i] = GetListSortDescription(prop_descs, properties [i]); } ApplySort(new ListSortDescriptionCollection(sort_descs)); } }
void IBindingList.ApplySort(PropertyDescriptor property, ListSortDirection direction) { ListSortDescription[] sorts = new ListSortDescription[1]; sorts[0] = new ListSortDescription(property, direction); ListSortDescriptionCollection lsdc = new ListSortDescriptionCollection(sorts); this.ApplySort(lsdc); }
/// <summary> /// Sorts the list based on a PropertyDescriptor and a ListSortDirection. /// </summary> /// <param name="property">The <see cref="PropertyDescriptor"/> to sort by.</param> /// <param name="direction">One of the <see cref="ListSortDirection"/> values.</param> public void ApplySort(PropertyDescriptor property, ListSortDirection direction) { ListSortDescription listSortDescription = new ListSortDescription(property, direction); ListSortDescription[] listSortDescriptions = new ListSortDescription[] { listSortDescription }; ListSortDescriptionCollection sorts = new ListSortDescriptionCollection(listSortDescriptions); ApplySort(sorts); }
protected virtual void OnApplySort(ListSortDescriptionCollection sorts) { this.sortOrder.Clear(); this.sortDescriptions = sorts; foreach (object obj in ((IEnumerable)sorts)) { ListSortDescription listSortDescription = (ListSortDescription)obj; this.sortOrder.Add(new QueueViewerSortOrderEntry(listSortDescription.PropertyDescriptor.Name, listSortDescription.SortDirection)); } this.InternalSort(); }
public ConfigObjectComparer(ListSortDescription sort) { if (sort == null) { throw new ArgumentNullException("sort"); } this.sorts = new ListSortDescriptionCollection(new ListSortDescription[] { sort }); }
protected Comparison <T> GetComparer(ListSortDescription sort) { if (__cComparers != null) { string sKey = sort.PropertyDescriptor.DisplayName + ((sort.SortDirection == ListSortDirection.Ascending) ? "_A" : "_D"); Comparison <T> cComparison = null; __cComparers.TryGetValue(sKey, out cComparison); return(cComparison); } return(null); }
public SortComparer(string[] fieldNames, ListSortDirection listSortDirection) { List <ListSortDescription> listSortDescriptions = new List <ListSortDescription>(); foreach (string fieldName in fieldNames) { PropertyDescriptor propertyDescriptor = TypeDescriptor.GetProperties(typeof(T))[fieldName]; ListSortDescription listSortDescription = new ListSortDescription(propertyDescriptor, listSortDirection); listSortDescriptions.Add(listSortDescription); } _listSortDescriptionCollection = new ListSortDescriptionCollection(listSortDescriptions.ToArray()); }
int RecursiveCompareInternal(T x, T y, int index) { if (index >= _SortCollection.Count) { return(0); } ListSortDescription listSortDesc = _SortCollection[index]; object xValue = listSortDesc.PropertyDescriptor.GetValue(x); object yValue = listSortDesc.PropertyDescriptor.GetValue(y); int retValue = CompareValues(xValue, yValue, listSortDesc.SortDirection); return((retValue == 0) ? RecursiveCompareInternal(x, y, ++index) : retValue); }
private int RecursiveCompareInternal(T x, T y, int index) { if (index >= this.m_SortCollection.Count) { return(0); } ListSortDescription description = this.m_SortCollection[index]; object xValue = description.PropertyDescriptor.GetValue(x); object yValue = description.PropertyDescriptor.GetValue(y); int num = this.CompareValues(xValue, yValue, description.SortDirection); if (num == 0) { return(this.RecursiveCompareInternal(x, y, ++index)); } return(num); }
private int RecursiveCompareInternal(T x, T y, int index) { if (index >= m_SortCollection.Count) { return(0); // termination condition } ListSortDescription listSortDesc = m_SortCollection[index]; object xValue = null; object yValue = null; //HACK: PropertyDescriptor.GetValue() is throwing an 'Object Does Not Match Target Type' // exception so I'm using the PropertyInfo instead. I can't figure out the cause. //PropertyDescriptor prop = listSortDesc.PropertyDescriptor; PropertyInfo prop = typeof(T).GetProperty(listSortDesc.PropertyDescriptor.Name); if (prop != null) { xValue = prop.GetValue(x, null); yValue = prop.GetValue(y, null); //object xValue = listSortDesc.PropertyDescriptor.GetValue(x); //object yValue = listSortDesc.PropertyDescriptor.GetValue(y); } if (xValue == null) { xValue = string.Empty; } if (yValue == null) { yValue = string.Empty; } int retValue = CompareValues(xValue, yValue, listSortDesc.SortDirection); if (retValue == 0) { return(RecursiveCompareInternal(x, y, ++index)); } else { return(retValue); } }
int IComparer.Compare(object x, object y) { int num = 0; if (!typeof(ConfigObject).IsAssignableFrom(x.GetType()) || !typeof(ConfigObject).IsAssignableFrom(x.GetType())) { throw new ArgumentException(); } if (x != null && y != null) { if (this.sorts != null) { using (IEnumerator enumerator = ((IEnumerable)this.sorts).GetEnumerator()) { while (enumerator.MoveNext()) { object obj = enumerator.Current; ListSortDescription listSortDescription = (ListSortDescription)obj; string name = listSortDescription.PropertyDescriptor.Name; num = this.CompareObjectProperties(x, y, listSortDescription.PropertyDescriptor.Name, listSortDescription.SortDirection == ListSortDirection.Descending); if (num != 0) { break; } } return(num); } } num = this.CompareObjectProperties(x, y, this.propertyToSort, this.sortDirection == ListSortDirection.Descending); } else if (x == null && y != null) { num = -1; } else if (y == null && x != null) { num = 1; } else { num = 0; } return(num); }
private int RecursiveCompareInternal(T x, T y, int index) { if (index >= _listSortDescriptionCollection.Count) { return(0); // termination condition } ListSortDescription listSortDesc = _listSortDescriptionCollection[index]; object xValue = listSortDesc.PropertyDescriptor.GetValue(x); object yValue = listSortDesc.PropertyDescriptor.GetValue(y); int retValue = CompareValues(xValue, yValue, listSortDesc.SortDirection); if (retValue == 0) { return(RecursiveCompareInternal(x, y, ++index)); } return(retValue); }
public ExDataView(DataTable dataSource, string sortProperty, ListSortDirection sortDirection) { if (dataSource == null) { throw new ArgumentNullException(); } this.parent = dataSource.DefaultView; this._dataSource = dataSource; if (!SupportsSorting || !SupportsAdvancedSorting) { throw new InvalidOperationException("Advanced sorting is required"); } fixedSort = new ListSortDescription(GetItemProperties(null)[sortProperty], sortDirection); RemoveSort(); }
/// <summary> /// 解析orderby字符串到ListSortDescription中 /// </summary> /// <param name="orderBy">类似Sql排序的排序串</param> /// <returns></returns> private ListSortDescriptionCollection ParseOrderBy(string orderBy) { if (orderBy == null || orderBy.Length == 0) { throw new ArgumentNullException("orderBy"); } string[] props = orderBy.Split(','); ListSortDescription[] sortProps = new ListSortDescription[props.Length]; string prop; ListSortDirection sortDirection = ListSortDirection.Ascending; for (int i = 0; i < props.Length; i++) { //Default to Ascending sortDirection = ListSortDirection.Ascending; prop = props[i].Trim(); if (prop.ToUpper().EndsWith(" DESC")) { sortDirection = ListSortDirection.Descending; prop = prop.Substring(0, prop.ToUpper().LastIndexOf(" DESC")); } else if (prop.ToUpper().EndsWith(" ASC")) { prop = prop.Substring(0, prop.ToUpper().LastIndexOf(" ASC")); } prop = prop.Trim(); //Get the appropriate descriptor PropertyDescriptor propertyDescriptor = m_PropertyDescriptors[prop]; if (propertyDescriptor == null) { throw new ArgumentException(string.Format("The property \"{0}\" is not a valid property.", prop)); } sortProps[i] = new ListSortDescription(propertyDescriptor, sortDirection); } return(new ListSortDescriptionCollection(sortProps)); }
public virtual void ApplySort(ListSortDescriptionCollection sorts) { if (sorts == null) { throw new ArgumentNullException("sorts"); } List <ListSortDescription> list = new List <ListSortDescription>(); foreach (object obj in ((IEnumerable)sorts)) { ListSortDescription listSortDescription = (ListSortDescription)obj; if (listSortDescription == null || listSortDescription.PropertyDescriptor == null || !this.DataTableLoader.Table.Columns.Contains(listSortDescription.PropertyDescriptor.Name)) { throw new ArgumentException("sorts"); } list.Add(new ListSortDescription(listSortDescription.PropertyDescriptor, listSortDescription.SortDirection)); } this.DataView.Sort = this.CreateSortString(sorts); this.SortDescriptions = new ListSortDescriptionCollection(list.ToArray()); }
private string CreateSortString(ListSortDescriptionCollection sorts) { StringBuilder stringBuilder = new StringBuilder(); bool flag = true; foreach (object obj in ((IEnumerable)sorts)) { ListSortDescription listSortDescription = (ListSortDescription)obj; if (!flag) { stringBuilder.Append(','); } stringBuilder.Append(this.CreateSortString(listSortDescription.PropertyDescriptor, listSortDescription.SortDirection)); if (flag) { flag = false; } } return(stringBuilder.ToString()); }
protected override int RecursiveCompareInternal(WorkUnitRow xVal, WorkUnitRow yVal, int index) { if (index >= SortDescriptions.Count) { return(0); // termination condition } /* Get property values */ ListSortDescription listSortDesc = SortDescriptions[index]; object xValue = listSortDesc.PropertyDescriptor.GetValue(xVal); object yValue = listSortDesc.PropertyDescriptor.GetValue(yVal); long xIdValue = xVal.ID; long yIdValue = yVal.ID; int result; /* Determine sort order */ if (listSortDesc.SortDirection == ListSortDirection.Ascending) { result = CompareAscending(xValue, yValue); } else { result = CompareDescending(xValue, yValue); } /* If the properties are equal, compare the next property */ if (result == 0) { result = RecursiveCompareInternal(xVal, yVal, ++index); } // if values are equal, sort via the entry id (asc) if (result == 0) { result = CompareAscending(xIdValue, yIdValue); } return(result); }
string GetSortExpression() { if (sorts.Count == 0) { return(string.Empty); } var sortexpr = new StringBuilder(); for (int i = 0; i < sorts.Count; i++) { if (i != 0) { sortexpr.Append(", "); } ListSortDescription lsd = sorts [i]; sortexpr.AppendFormat("{0} {1}", lsd.PropertyDescriptor.Name, (lsd.SortDirection == ListSortDirection.Ascending) ? "ASC" : "DESC"); } return(sortexpr.ToString()); }
/// <summary> /// Compares two objects and returns a item indicating whether one is less than, equal to, or greater than the other. /// </summary> /// <param name="x">The first object to compare.</param> /// <param name="y">The second object to compare.</param> /// <returns> /// Value Condition Less than zero<paramref name="x" /> is less than <paramref name="y" />.Zero<paramref name="x" /> equals <paramref name="y" />.Greater than zero<paramref name="x" /> is greater than <paramref name="y" />. /// </returns> protected virtual int OnCompare(IEntityItemView <T> x, IEntityItemView <T> y) { /* * Il confronto viene fatto scorrendo tutti * i ListSortDescription... * * Diventa necessario confrontare i criteri successivi * solo se il precedente non ha dato risultati utili, cioè * i due valori confrontati sono uguali... * * Il classico esempio è Cognome ASC, Nome DESC confronto i * nomi solo se i Cognomi sono uguali */ int retVal = 0; if (this.SortDescriptions.Count == 1) { ListSortDescription sd = this.SortDescriptions[0]; object valueX = sd.PropertyDescriptor.GetValue(x); object valueY = sd.PropertyDescriptor.GetValue(y); retVal = EntityItemViewSortComparer <T> .Compare(valueX, valueY, sd.SortDirection); } else { foreach (ListSortDescription sd in this.SortDescriptions) { if (retVal == 0) { object valueX = sd.PropertyDescriptor.GetValue(x); object valueY = sd.PropertyDescriptor.GetValue(y); retVal = EntityItemViewSortComparer <T> .Compare(valueX, valueY, sd.SortDirection); } } } return(retVal); }
private int RecursiveCompareInternal(T x, T y, int index) { if (index >= m_SortCollection.Count)//递归比较结束条件 { return(0); } ListSortDescription listSortDesc = m_SortCollection[index]; object xValue = listSortDesc.PropertyDescriptor.GetValue(x); object yValue = listSortDesc.PropertyDescriptor.GetValue(y); int retValue = CompareValues(xValue, yValue, listSortDesc.SortDirection); if (retValue == 0) { return(RecursiveCompareInternal(x, y, ++index)); } else { return(retValue); } }
public ListSortDescriptionCollection(ListSortDescription[] sorts) {}