mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 05:05:05 +00:00
Initial commit of PropertyComparer
This commit is contained in:
30
ICD.Common.Utils/Comparers/PropertyComparer.cs
Normal file
30
ICD.Common.Utils/Comparers/PropertyComparer.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace ICD.Common.Utils.Comparers
|
||||
{
|
||||
public sealed class PropertyComparer<TParent, TProperty> : IComparer<TParent>
|
||||
{
|
||||
private readonly IComparer<TProperty> m_Comparer;
|
||||
private readonly Func<TParent, TProperty> m_GetProperty;
|
||||
|
||||
/// <summary>
|
||||
/// Constructor.
|
||||
/// </summary>
|
||||
/// <param name="comparer"></param>
|
||||
/// <param name="getProperty"></param>
|
||||
public PropertyComparer(IComparer<TProperty> comparer, Func<TParent, TProperty> getProperty)
|
||||
{
|
||||
m_Comparer = comparer;
|
||||
m_GetProperty = getProperty;
|
||||
}
|
||||
|
||||
public int Compare(TParent x, TParent y)
|
||||
{
|
||||
TProperty a = m_GetProperty(x);
|
||||
TProperty b = m_GetProperty(y);
|
||||
|
||||
return m_Comparer.Compare(a, b);
|
||||
}
|
||||
}
|
||||
}
|
||||
41
ICD.Common.Utils/Comparers/PropertyEqualityComparer.cs
Normal file
41
ICD.Common.Utils/Comparers/PropertyEqualityComparer.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace ICD.Common.Utils.Comparers
|
||||
{
|
||||
/// <summary>
|
||||
/// Allows for comparing items based on some property.
|
||||
/// </summary>
|
||||
/// <typeparam name="TParent"></typeparam>
|
||||
/// <typeparam name="TProperty"></typeparam>
|
||||
public sealed class PropertyEqualityComparer<TParent, TProperty> : IEqualityComparer<TParent>
|
||||
{
|
||||
private readonly IEqualityComparer<TProperty> m_Comparer;
|
||||
private readonly Func<TParent, TProperty> m_GetProperty;
|
||||
|
||||
/// <summary>
|
||||
/// Constructor.
|
||||
/// </summary>
|
||||
/// <param name="comparer"></param>
|
||||
/// <param name="getProperty"></param>
|
||||
public PropertyEqualityComparer(IEqualityComparer<TProperty> comparer, Func<TParent, TProperty> getProperty)
|
||||
{
|
||||
m_Comparer = comparer;
|
||||
m_GetProperty = getProperty;
|
||||
}
|
||||
|
||||
public bool Equals(TParent x, TParent y)
|
||||
{
|
||||
TProperty a = m_GetProperty(x);
|
||||
TProperty b = m_GetProperty(y);
|
||||
|
||||
return m_Comparer.Equals(a, b);
|
||||
}
|
||||
|
||||
public int GetHashCode(TParent parent)
|
||||
{
|
||||
TProperty property = m_GetProperty(parent);
|
||||
return m_Comparer.GetHashCode(property);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user