我正在尝试编写一个动态函数,它接受一个自定义类的集合,并根据参数中指定的属性对集合进行排序。
目前看起来像这样:
public void AscendingSort(ObservableCollection<ManifestItem> list, string property)
{
try
{
List<ManifestItem> temp = list.ToList();
//set the global equal to the result
manifestData.Manifest_Items = new ObservableCollection<ManifestItem>(temp.OrderBy(x => x.GetType().GetProperty(property).GetValue(x)).ToList());
}
catch (Exception e)
{
MessageBox.Show("Error in Manifest AscendingSort");
MessageBox.Show(e.Message.ToString());
}
}
这是我的ManifestItem类
public class ManifestItem : INotifyPropertyChanged
{
[JsonProperty("ttl")]
private int ttl;
public int TTL
{
get
{
return ttl;
}
set
{
if (ttl != value)
{
ttl = value;
RaisePropertyChanged("TTL");
}
}
}
[JsonProperty("serial")]
private object serial;
public object SataType
{
get
{
return serial;
}
set
{
if (serial != value)
{
serial = value;
RaisePropertyChanged("Serial");
}
}
}
[JsonProperty("id")]
private string id;
public string ID
{
get
{
return id;
}
set
{
if (id != value)
{
id = value;
RaisePropertyChanged("ID");
}
}
}
[JsonProperty("formatType")]
private string formatType;
public string FormatType
{
get
{
return formatType;
}
set
{
if (formatType != value)
{
formatType = value;
RaisePropertyChanged("FormatType");
}
}
}
[JsonProperty("dataType")]
private string dataType;
public string DataType
{
get
{
return dataType;
}
set
{
if (dataType != value)
{
dataType = value;
RaisePropertyChanged("DataType");
}
}
}
[JsonProperty("modifiedIso8601")]
private string modifiedIso8601;
public string ModifiedIso8601
{
get
{
return modifiedIso8601;
}
set
{
if (modifiedIso8601 != value)
{
modifiedIso8601 = value;
RaisePropertyChanged("ModifiedIso8601");
}
}
}
[JsonProperty("modifiedTimestamp")]
private object modifiedTimestamp;
public object ModifiedTimestamp
{
get
{
return modifiedTimestamp;
}
set
{
if (modifiedTimestamp != value)
{
modifiedTimestamp = value;
RaisePropertyChanged("ModifiedTimestamp");
}
}
}
[JsonProperty("link")]
private Link link;
public Link Link
{
get
{
return link;
}
set
{
if (link != value)
{
link = value;
RaisePropertyChanged("Link");
}
}
}
[JsonProperty("returnChannels")]
private ObservableCollection<object> returnChannels;
public ObservableCollection<object> ReturnChannels
{
get
{
return returnChannels;
}
set
{
if (returnChannels != value)
{
returnChannels = value;
RaisePropertyChanged("ReturnChannels");
}
}
}
//Event handling
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string property)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
}
我的问题是它不是自然排序顺序,所以我明白了:
而不是:
由于x是我班级的一个实例,我无法以任何方式处理它。如果不编写IComparer,我可以这样做吗?我已经尝试编写一个自定义IComparer,但在第一个障碍时堕落,因为我需要找到一种方法将指定的属性包含在我的比较中。
答案 0 :(得分:1)
首先,将反射内容移动到闭包中,这样就不会为每个项目重新计算它了:
var propertyInfo = x.GetType().GetProperty(property);
manifestData.Manifest_Items = new ObservableCollection<ManifestItem>
(
temp.OrderBy(x => propertyInfo.GetValue(x)).ToList()
);
要获得所需的排序,您应该实现IComparer
,但如果您想要这个丑陋的解决方案,那就是:
var propertyInfo = x.GetType().GetProperty(property);
manifestData.Manifest_Items = new ObservableCollection<ManifestItem>
(
temp.OrderBy(x =>
{
var s = propertyInfo.GetValue(x);
return s.Substring(0, 1) + s.Substring(1).PadLeft(4, '0');
}).ToList()
);
没什么特别的。该解决方案将容纳从A1到Z9999的数字。