Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / wpf / src / Framework / MS / Internal / Data / SortFieldComparer.cs / 1305600 / SortFieldComparer.cs
//---------------------------------------------------------------------------- // //// Copyright (C) 2003 by Microsoft Corporation. All rights reserved. // // // // Description: IComparer class to sort by class property value (using reflection). // // See spec at http://avalon/connecteddata/M5%20Specs/IDataCollection.mht // // History: // 06/02/2003 : [....] - Created // //--------------------------------------------------------------------------- using System; using System.ComponentModel; using System.Collections; using System.Collections.Generic; using System.Reflection; using System.Globalization; using MS.Utility; using System.Windows; namespace MS.Internal.Data { ////// IComparer class to sort by class property value (using reflection). /// internal class SortFieldComparer : IComparer { ////// Create a comparer, using the SortDescription and a Type; /// tries to find a reflection PropertyInfo for each property name /// /// list of property names and direction to sort by /// culture to use for comparisons internal SortFieldComparer(SortDescriptionCollection sortFields, CultureInfo culture) { _sortFields = sortFields; _fields = CreatePropertyInfo(_sortFields); // create the comparer _comparer = (culture == null || culture == CultureInfo.InvariantCulture) ? Comparer.DefaultInvariant : (culture == CultureInfo.CurrentCulture) ? Comparer.Default : new Comparer(culture); } ////// Compares two objects and returns a value indicating whether one is less than, equal to or greater than the other. /// /// first item to compare /// second item to compare ///; <0: o1 < o2; =0: o1 == o2; > 0: o1 > o2 ////// Compares the 2 items using the list of property names and directions. /// public int Compare(object o1, object o2) { int result = 0; // compare both objects by each of the properties until property values don't match for (int k = 0; k < _fields.Length; ++k) { object v1 = _fields[k].GetValue(o1); object v2 = _fields[k].GetValue(o2); result = _comparer.Compare(v1, v2); if (_fields[k].descending) result = -result; if (result != 0) break; } return result; } // Helper method for sorting an ArrayList. If the comparer is a SortFieldComparer, // use the cached-value approach to avoid excessive reflection. For other // comparers, sort the usual way internal static void SortHelper(ArrayList al, IComparer comparer) { SortFieldComparer sfc = comparer as SortFieldComparer; if (sfc == null) { // sort the usual way al.Sort(comparer); } else { // Sort with cached values. // Step 1. Copy the items into a list augmented with slots for // the cached values. int n = al.Count; int nFields = sfc._fields.Length; CachedValueItem[] list = new CachedValueItem[n]; for (int i=0; i// Copyright (C) 2003 by Microsoft Corporation. All rights reserved. // // // // Description: IComparer class to sort by class property value (using reflection). // // See spec at http://avalon/connecteddata/M5%20Specs/IDataCollection.mht // // History: // 06/02/2003 : [....] - Created // //--------------------------------------------------------------------------- using System; using System.ComponentModel; using System.Collections; using System.Collections.Generic; using System.Reflection; using System.Globalization; using MS.Utility; using System.Windows; namespace MS.Internal.Data { /// /// IComparer class to sort by class property value (using reflection). /// internal class SortFieldComparer : IComparer { ////// Create a comparer, using the SortDescription and a Type; /// tries to find a reflection PropertyInfo for each property name /// /// list of property names and direction to sort by /// culture to use for comparisons internal SortFieldComparer(SortDescriptionCollection sortFields, CultureInfo culture) { _sortFields = sortFields; _fields = CreatePropertyInfo(_sortFields); // create the comparer _comparer = (culture == null || culture == CultureInfo.InvariantCulture) ? Comparer.DefaultInvariant : (culture == CultureInfo.CurrentCulture) ? Comparer.Default : new Comparer(culture); } ////// Compares two objects and returns a value indicating whether one is less than, equal to or greater than the other. /// /// first item to compare /// second item to compare ///; <0: o1 < o2; =0: o1 == o2; > 0: o1 > o2 ////// Compares the 2 items using the list of property names and directions. /// public int Compare(object o1, object o2) { int result = 0; // compare both objects by each of the properties until property values don't match for (int k = 0; k < _fields.Length; ++k) { object v1 = _fields[k].GetValue(o1); object v2 = _fields[k].GetValue(o2); result = _comparer.Compare(v1, v2); if (_fields[k].descending) result = -result; if (result != 0) break; } return result; } // Helper method for sorting an ArrayList. If the comparer is a SortFieldComparer, // use the cached-value approach to avoid excessive reflection. For other // comparers, sort the usual way internal static void SortHelper(ArrayList al, IComparer comparer) { SortFieldComparer sfc = comparer as SortFieldComparer; if (sfc == null) { // sort the usual way al.Sort(comparer); } else { // Sort with cached values. // Step 1. Copy the items into a list augmented with slots for // the cached values. int n = al.Count; int nFields = sfc._fields.Length; CachedValueItem[] list = new CachedValueItem[n]; for (int i=0; i
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- FileSecurity.cs
- UserPreferenceChangingEventArgs.cs
- ModelPropertyImpl.cs
- RectangleHotSpot.cs
- EncryptedKeyIdentifierClause.cs
- AtomServiceDocumentSerializer.cs
- FlowDocumentReaderAutomationPeer.cs
- ServiceInstallComponent.cs
- SqlGenericUtil.cs
- EntryWrittenEventArgs.cs
- MessagePropertyDescriptionCollection.cs
- PrintPageEvent.cs
- StylesEditorDialog.cs
- PageRequestManager.cs
- SSmlParser.cs
- ClipboardData.cs
- ZoneButton.cs
- PageContentCollection.cs
- ByteStorage.cs
- _IPv4Address.cs
- AddressHeaderCollectionElement.cs
- ObjectStateFormatter.cs
- OledbConnectionStringbuilder.cs
- ListParaClient.cs
- DataGridViewCellStyle.cs
- PackWebResponse.cs
- RowVisual.cs
- BuildProviderCollection.cs
- UnsafeNativeMethods.cs
- Operator.cs
- DataTrigger.cs
- GB18030Encoding.cs
- _KerberosClient.cs
- Constraint.cs
- NameValueFileSectionHandler.cs
- SvcMapFileSerializer.cs
- SemanticTag.cs
- ContextQuery.cs
- WindowsAltTab.cs
- AccessKeyManager.cs
- EditCommandColumn.cs
- ResXDataNode.cs
- DataTableTypeConverter.cs
- UnmanagedMemoryStream.cs
- CompensationTokenData.cs
- TableLayoutPanel.cs
- SecurityTokenProviderContainer.cs
- HtmlEncodedRawTextWriter.cs
- DataContract.cs
- TextTreeTextBlock.cs
- SettingsSection.cs
- DecimalAverageAggregationOperator.cs
- MethodBuilder.cs
- XmlnsDefinitionAttribute.cs
- Geometry3D.cs
- PolyQuadraticBezierSegment.cs
- MILUtilities.cs
- HashMembershipCondition.cs
- Light.cs
- FixUpCollection.cs
- ListView.cs
- DynamicMethod.cs
- DesignConnectionCollection.cs
- ForceCopyBuildProvider.cs
- EventMap.cs
- InputEventArgs.cs
- BuilderPropertyEntry.cs
- DataTableMappingCollection.cs
- DataGridHelper.cs
- FtpRequestCacheValidator.cs
- PolicyValidationException.cs
- ScriptingSectionGroup.cs
- EnumValidator.cs
- DataGridViewColumn.cs
- TogglePatternIdentifiers.cs
- Asn1IntegerConverter.cs
- AddInDeploymentState.cs
- OverflowException.cs
- XmlEncodedRawTextWriter.cs
- CollectionViewProxy.cs
- EdgeProfileValidation.cs
- Material.cs
- SiteMembershipCondition.cs
- ParseNumbers.cs
- ErasingStroke.cs
- FtpWebRequest.cs
- PackagePartCollection.cs
- ResourcesGenerator.cs
- ResponseBodyWriter.cs
- GACMembershipCondition.cs
- XmlSchemaAll.cs
- OdbcEnvironmentHandle.cs
- TextElementEditingBehaviorAttribute.cs
- SessionEndedEventArgs.cs
- ModelPropertyImpl.cs
- XmlDataLoader.cs
- TerminateSequenceResponse.cs
- XmlSchemaValidationException.cs
- SecurityCriticalDataForSet.cs
- DefaultAuthorizationContext.cs