以特定顺序比较器对列表进行排序
Sort a list in a specific order comparer
我有一个字符串列表,我需要编写一个 IComparer
实现来按特定顺序对它们进行排序。
我当前的实现:
public enum TimeBucket
{
[Description("0D")]
ZeroDay,
[Description("1D")]
OneDay,
[Description("1W")]
OneWeek,
[Description("2W")]
TwoWeek,
[Description("0M")]
ZeroMonth,
[Description("1M")]
OneMonth
}
public class TimeBucketComparer : IComparer
{
public static TimeBucketComparer Instance { get; } = new TimeBucketComparer();
private TimeBucketComparer()
{
}
public int Compare(object x, object y)
{
TimeBucket xvar = GetValue(string.Join("", x.ToString().Split(' ')));
TimeBucket yvar = GetValue(string.Join("", y.ToString().Split(' ')));
if (EqualityComparer<TimeBucket>.Default.Equals(xvar, default(TimeBucket)) &&
EqualityComparer<TimeBucket>.Default.Equals(yvar, default(TimeBucket)))
return String.CompareOrdinal(xvar.ToString(), yvar.ToString());
if (EqualityComparer<TimeBucket>.Default.Equals(xvar, default(TimeBucket))) return -1;
if (EqualityComparer<TimeBucket>.Default.Equals(yvar, default(TimeBucket))) return 1;
return xvar.CompareTo(yvar);
}
public TimeBucket GetValue(string description) => EnumExtensions.GetValueFromDescription<TimeBucket>(description);
}
public static class EnumExtensions
{
public static string GetDescription(this Enum value)
{
return ((DescriptionAttribute)Attribute.GetCustomAttribute(value.GetType().GetFields(BindingFlags.Public | BindingFlags.Static).Single(x => x.GetValue(null).Equals(value)),typeof(DescriptionAttribute)))?.Description ?? value.ToString();
}
public static T GetValueFromDescription<T>(string description)
{
var type = typeof(T);
if (!type.IsEnum) throw new InvalidOperationException();
foreach (var field in type.GetFields())
{
var attribute = Attribute.GetCustomAttribute(field,
typeof(DescriptionAttribute)) as DescriptionAttribute;
if (attribute != null)
{
if (attribute.Description == description)
return (T)field.GetValue(null);
}
else
{
if (field.Name == description)
return (T)field.GetValue(null);
}
}
throw new ArgumentException("Not found.", "description");
// or return default(T);
}
}
由于涉及反射,我当前的实现需要花费大量时间。我使用这种方式是因为需要一个通用的实现。
输入至少有 40000 条需要排序的 Timebuckets 记录。排序算法是 linq orderby,它是通用的,不能修改。因此,需要使用比较器。
我需要对字符串进行排序。有没有更好的不使用反射的方法?
编辑:如果不清楚,我的输入是 {"1M", "1D", "1W", "0D"} 我需要的输出是 {"0D", "1D", "1W ", "1M"}
像这样定义枚举:
public enum TimeBucket
{
[Description("0D")]
ZeroDay = 0,
[Description("1D")]
OneDay = 1,
[Description("1W")]
OneWeek = 2,
[Description("2W")]
TwoWeek = 3,
[Description("0M")]
ZeroMonth = 4,
[Description("1M")]
OneMonth = 5
}
假设此枚举是某个实体的 属性,并且您想对该实体的集合进行排序。
例如:
var sortedList = coll.OrderBy(x => (int) (x.TimeBucket)).ToList();
我没有得到你的排序逻辑,但无论如何 - 如果你因为反射而遇到性能问题 - 只需进行一次反射并缓存结果。例如:
public class TimeBucketComparer : IComparer, IComparer<string> {
public static TimeBucketComparer Instance { get; } = new TimeBucketComparer();
private static readonly Lazy<Dictionary<string, TimeBucket>> _values = new Lazy<Dictionary<string, TimeBucket>>(() => {
// get all fields and store in dictionary, keyed by description attribute
return typeof(TimeBucket)
.GetFields(BindingFlags.Public | BindingFlags.Static)
.ToDictionary(c =>
c.GetCustomAttribute<DescriptionAttribute>()?.Description ?? c.Name,
c => (TimeBucket) c.GetValue(null));
});
private TimeBucketComparer() {
}
public int Compare(object x, object y) {
string xvar = string.Join("", x.ToString().Split(' '));
string yvar = string.Join("", y.ToString().Split(' '));
return Compare(xvar, yvar);
}
public int Compare(string x, string y) {
if (!_values.Value.ContainsKey(x))
{
// do something, invalid value
throw new ArgumentException(nameof(x));
}
if (!_values.Value.ContainsKey(y))
{
// do something, invalid value
throw new ArgumentException(nameof(y));
}
return _values.Value[x].CompareTo(_values.Value[y]);
}
}
我有一个字符串列表,我需要编写一个 IComparer
实现来按特定顺序对它们进行排序。
我当前的实现:
public enum TimeBucket
{
[Description("0D")]
ZeroDay,
[Description("1D")]
OneDay,
[Description("1W")]
OneWeek,
[Description("2W")]
TwoWeek,
[Description("0M")]
ZeroMonth,
[Description("1M")]
OneMonth
}
public class TimeBucketComparer : IComparer
{
public static TimeBucketComparer Instance { get; } = new TimeBucketComparer();
private TimeBucketComparer()
{
}
public int Compare(object x, object y)
{
TimeBucket xvar = GetValue(string.Join("", x.ToString().Split(' ')));
TimeBucket yvar = GetValue(string.Join("", y.ToString().Split(' ')));
if (EqualityComparer<TimeBucket>.Default.Equals(xvar, default(TimeBucket)) &&
EqualityComparer<TimeBucket>.Default.Equals(yvar, default(TimeBucket)))
return String.CompareOrdinal(xvar.ToString(), yvar.ToString());
if (EqualityComparer<TimeBucket>.Default.Equals(xvar, default(TimeBucket))) return -1;
if (EqualityComparer<TimeBucket>.Default.Equals(yvar, default(TimeBucket))) return 1;
return xvar.CompareTo(yvar);
}
public TimeBucket GetValue(string description) => EnumExtensions.GetValueFromDescription<TimeBucket>(description);
}
public static class EnumExtensions
{
public static string GetDescription(this Enum value)
{
return ((DescriptionAttribute)Attribute.GetCustomAttribute(value.GetType().GetFields(BindingFlags.Public | BindingFlags.Static).Single(x => x.GetValue(null).Equals(value)),typeof(DescriptionAttribute)))?.Description ?? value.ToString();
}
public static T GetValueFromDescription<T>(string description)
{
var type = typeof(T);
if (!type.IsEnum) throw new InvalidOperationException();
foreach (var field in type.GetFields())
{
var attribute = Attribute.GetCustomAttribute(field,
typeof(DescriptionAttribute)) as DescriptionAttribute;
if (attribute != null)
{
if (attribute.Description == description)
return (T)field.GetValue(null);
}
else
{
if (field.Name == description)
return (T)field.GetValue(null);
}
}
throw new ArgumentException("Not found.", "description");
// or return default(T);
}
}
由于涉及反射,我当前的实现需要花费大量时间。我使用这种方式是因为需要一个通用的实现。 输入至少有 40000 条需要排序的 Timebuckets 记录。排序算法是 linq orderby,它是通用的,不能修改。因此,需要使用比较器。
我需要对字符串进行排序。有没有更好的不使用反射的方法?
编辑:如果不清楚,我的输入是 {"1M", "1D", "1W", "0D"} 我需要的输出是 {"0D", "1D", "1W ", "1M"}
像这样定义枚举:
public enum TimeBucket
{
[Description("0D")]
ZeroDay = 0,
[Description("1D")]
OneDay = 1,
[Description("1W")]
OneWeek = 2,
[Description("2W")]
TwoWeek = 3,
[Description("0M")]
ZeroMonth = 4,
[Description("1M")]
OneMonth = 5
}
假设此枚举是某个实体的 属性,并且您想对该实体的集合进行排序。
例如:
var sortedList = coll.OrderBy(x => (int) (x.TimeBucket)).ToList();
我没有得到你的排序逻辑,但无论如何 - 如果你因为反射而遇到性能问题 - 只需进行一次反射并缓存结果。例如:
public class TimeBucketComparer : IComparer, IComparer<string> {
public static TimeBucketComparer Instance { get; } = new TimeBucketComparer();
private static readonly Lazy<Dictionary<string, TimeBucket>> _values = new Lazy<Dictionary<string, TimeBucket>>(() => {
// get all fields and store in dictionary, keyed by description attribute
return typeof(TimeBucket)
.GetFields(BindingFlags.Public | BindingFlags.Static)
.ToDictionary(c =>
c.GetCustomAttribute<DescriptionAttribute>()?.Description ?? c.Name,
c => (TimeBucket) c.GetValue(null));
});
private TimeBucketComparer() {
}
public int Compare(object x, object y) {
string xvar = string.Join("", x.ToString().Split(' '));
string yvar = string.Join("", y.ToString().Split(' '));
return Compare(xvar, yvar);
}
public int Compare(string x, string y) {
if (!_values.Value.ContainsKey(x))
{
// do something, invalid value
throw new ArgumentException(nameof(x));
}
if (!_values.Value.ContainsKey(y))
{
// do something, invalid value
throw new ArgumentException(nameof(y));
}
return _values.Value[x].CompareTo(_values.Value[y]);
}
}