Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / Orcas / NetFXw7 / wpf / src / Framework / MS / Internal / Data / SortFieldComparer.cs / 1 / 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;
}
// Private Methods
private SortPropertyInfo[] CreatePropertyInfo(SortDescriptionCollection sortFields)
{
SortPropertyInfo[] fields = new SortPropertyInfo[sortFields.Count];
for (int k = 0; k < sortFields.Count; ++k)
{
PropertyPath pp;
if (String.IsNullOrEmpty(sortFields[k].PropertyName))
{
// sort by the object itself (as opposed to a property)
pp = null;
}
else
{
// sort by the value of a property path, to be applied to
// the items in the list
pp = new PropertyPath(sortFields[k].PropertyName);
}
// remember PropertyPath and direction, used when actually sorting
fields[k].info = pp;
fields[k].descending = (sortFields[k].Direction == ListSortDirection.Descending);
}
return fields;
}
// Private Fields
struct SortPropertyInfo
{
internal PropertyPath info;
internal bool descending;
internal object GetValue(object o)
{
object value;
if (info == null)
{
value = o;
}
else
{
using (info.SetContext(o))
{
value = info.GetValue();
}
}
return value;
}
}
SortPropertyInfo[] _fields;
SortDescriptionCollection _sortFields;
Comparer _comparer;
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
//----------------------------------------------------------------------------
//
//
// 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;
}
// Private Methods
private SortPropertyInfo[] CreatePropertyInfo(SortDescriptionCollection sortFields)
{
SortPropertyInfo[] fields = new SortPropertyInfo[sortFields.Count];
for (int k = 0; k < sortFields.Count; ++k)
{
PropertyPath pp;
if (String.IsNullOrEmpty(sortFields[k].PropertyName))
{
// sort by the object itself (as opposed to a property)
pp = null;
}
else
{
// sort by the value of a property path, to be applied to
// the items in the list
pp = new PropertyPath(sortFields[k].PropertyName);
}
// remember PropertyPath and direction, used when actually sorting
fields[k].info = pp;
fields[k].descending = (sortFields[k].Direction == ListSortDirection.Descending);
}
return fields;
}
// Private Fields
struct SortPropertyInfo
{
internal PropertyPath info;
internal bool descending;
internal object GetValue(object o)
{
object value;
if (info == null)
{
value = o;
}
else
{
using (info.SetContext(o))
{
value = info.GetValue();
}
}
return value;
}
}
SortPropertyInfo[] _fields;
SortDescriptionCollection _sortFields;
Comparer _comparer;
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- SkinBuilder.cs
- Funcletizer.cs
- ClientProtocol.cs
- HttpTransportManager.cs
- LocalBuilder.cs
- InputLanguageProfileNotifySink.cs
- TabPanel.cs
- Activator.cs
- QilInvokeLateBound.cs
- SizeConverter.cs
- Stroke.cs
- ListSortDescription.cs
- IERequestCache.cs
- Stroke.cs
- SelectionRangeConverter.cs
- _Win32.cs
- PhysicalAddress.cs
- externdll.cs
- NodeLabelEditEvent.cs
- PropertyItem.cs
- Tablet.cs
- ItemCheckEvent.cs
- MediaTimeline.cs
- InstalledVoice.cs
- OdbcEnvironmentHandle.cs
- BamlLocalizabilityResolver.cs
- CalendarTable.cs
- EditingCoordinator.cs
- ObjectFullSpanRewriter.cs
- FilterElement.cs
- CheckBoxAutomationPeer.cs
- MissingManifestResourceException.cs
- SingleConverter.cs
- MatrixValueSerializer.cs
- RestClientProxyHandler.cs
- UpDownBase.cs
- Binding.cs
- LogLogRecord.cs
- RowToFieldTransformer.cs
- DefaultAssemblyResolver.cs
- UserControlCodeDomTreeGenerator.cs
- DocumentPageView.cs
- PerformanceCounterLib.cs
- DataGridPageChangedEventArgs.cs
- CurrentTimeZone.cs
- SiteMapPathDesigner.cs
- SafeArrayTypeMismatchException.cs
- StorageEntityContainerMapping.cs
- MetadataArtifactLoaderComposite.cs
- HtmlSelect.cs
- EventLogQuery.cs
- Predicate.cs
- BufferedGraphicsManager.cs
- AliasedSlot.cs
- CodeTypeReferenceExpression.cs
- ProfileSettings.cs
- Maps.cs
- SelectorAutomationPeer.cs
- ColumnCollection.cs
- FontFaceLayoutInfo.cs
- SqlCacheDependency.cs
- FreezableOperations.cs
- DoubleAnimationUsingKeyFrames.cs
- CryptoStream.cs
- XmlSchemaAll.cs
- DbMetaDataColumnNames.cs
- GroupBoxAutomationPeer.cs
- ThreadInterruptedException.cs
- XmlExceptionHelper.cs
- CssClassPropertyAttribute.cs
- SerialPort.cs
- BindingListCollectionView.cs
- HandleTable.cs
- HttpHandlersInstallComponent.cs
- WrappedIUnknown.cs
- DesignerAutoFormat.cs
- PropertyDescriptorGridEntry.cs
- TextDecorationUnitValidation.cs
- NonParentingControl.cs
- EmptyEnumerator.cs
- LazyTextWriterCreator.cs
- CatalogZoneBase.cs
- linebase.cs
- XPathNodeList.cs
- HyperLinkField.cs
- LogEntrySerializationException.cs
- SimpleTypeResolver.cs
- StylusShape.cs
- WebPartCatalogAddVerb.cs
- DragCompletedEventArgs.cs
- WindowShowOrOpenTracker.cs
- SamlSecurityTokenAuthenticator.cs
- ServiceNameElementCollection.cs
- BuildDependencySet.cs
- ResourceType.cs
- Internal.cs
- LogicalExpr.cs
- OptimizedTemplateContent.cs
- MyContact.cs
- SchemaImporterExtensionsSection.cs