/// <summary> /// Inserts an item in order. /// </summary> /// <param name="item">The item to insert.</param> /// <param name="lowIndex">The index of the lowest item to check.</param> /// <param name="highIndex">One more than the highest index to check.</param> /// <returns>The sorted index of the item.</returns> private int InsertInOrder(ObjectView item, int lowIndex, int highIndex) { if (_sorts == null || _sorts.Count == 0 || _sortIndex.Count == 0) { _sortIndex.Add(item); return _sortIndex.Count - 1; } if (lowIndex == highIndex) { // We have gotten to the end of our test if (highIndex == _sortIndex.Count) { _sortIndex.Add(item); return _sortIndex.Count - 1; } else { _sortIndex.Insert(highIndex, item); return highIndex; } } int testIndex = (lowIndex + highIndex) / 2; int comparison = CompareObject(item, _sortIndex[testIndex]); if (comparison == 0) { _sortIndex.Insert(testIndex, item); return testIndex; } else if (comparison < 0) { return this.InsertInOrder(item, lowIndex, testIndex); } else { return this.InsertInOrder(item, testIndex + 1, highIndex); } }
/// <summary> /// A helper method that compares items in the list. /// </summary> /// <param name="objectA">The first item.</param> /// <param name="objectB">The second item.</param> /// <returns> /// A 32-bit signed integer that indicates the relative order of the objects being compared. /// The return value has these meanings: Less than zero objectA should be before objectB. /// Zero the order of objectA and objectB should not be changed. /// Greater than zero objectA should be after objectB. /// </returns> private int CompareObject(ObjectView objectA, ObjectView objectB) { if (_sorts == null || (objectA == null && objectB == null)) { return 0; } else if (objectA == null) { return -1; } else if (objectB == null) { return 1; } int comparison = 0; for (int i = 0; i < _sorts.Count; i++) { PropertyDescriptor prop = _sorts[i].PropertyDescriptor; object valueA = objectA[prop.Name]; object valueB = objectB[prop.Name]; if (objectA.Object is IExtendSort) valueA = ((IExtendSort)objectA.Object).GetSortValue(prop, valueA); if (objectB.Object is IExtendSort) valueB = ((IExtendSort)objectB.Object).GetSortValue(prop, valueB); this.OnExtendSort(prop, valueA); this.OnExtendSort(prop, valueB); comparison = this.Compare(valueA, valueB); if (comparison != 0) { if (_sorts[i].SortDirection == ListSortDirection.Descending) { comparison *= -1; } break; } } return comparison; }