由网友(盐不及泪咸°)分享简介:我有以下现在:开关(Mysort){案reqDate:lstDMV.Sort((X,Y)=> DateTime.Compare(x.RequestDate,y.RequestDate));打破;案notifDate:lstDMV.Sort((X,Y)=> DateTime.Compare(x.NotifD...![用Excel求两个变量的值162X 252Y 1672求X Y的值 值为整数](https://p.xsw88.cn/allimgs/daicuo/20230903/5291.png)
我有以下现在:
开关(Mysort)
{
案reqDate:
lstDMV.Sort((X,Y)=> DateTime.Compare(x.RequestDate,y.RequestDate));
打破;
案notifDate:
lstDMV.Sort((X,Y)=> DateTime.Compare(x.NotifDate,y.NotifDate));
打破;
案的dueDate:
lstDMV.Sort((X,Y)=>的String.Compare(x.TargetDateShort,y.TargetDateShort));
打破;
案天:
lstDMV.Sort((X,Y)=> x.DaysLapsed.CompareTo(y.DaysLapsed));
打破;
}
我想摆脱的case语句,只是这样做:
lstDMV.Sort((X,Y)=>的String.Compare(x.MySort,y.MySort));
case语句是巨大的,它真的会减少可读性。但由于 MySort
不包含在 lstDMV
它不工作。有另一种方式我可以代替它?
我当然会改变文本,以确保 MySort
变量的值精确匹配的 lstDMV
属性名称。
我也试了下,没有运气:(
如果(排序!=)
{
串xsort,ysort;
xsort =×。 +排序;
ysort =Y。 +排序;
lstDMV.Sort((X,Y)=>的String.Compare(xsort,ysort));
}
![用Excel求两个变量的值162X 252Y 1672求X Y的值 值为整数](https://p.xsw88.cn/allimgs/daicuo/20230903/5291.png)
解决方案
一个与词典比较器函数功能的
公共类YourDataClass {
公共字符串RequestDate {获得;组; }
公共字符串NotifDate {获得;组; }
。
。
。
}
公共类分拣机< T>其中T:YourDataClass {
私人字典<字符串函数功能< T,T,INT>>行动=
新的字典<字符串函数功能< T,T,INT>> {
{reqDate,(X,Y)=>的String.Compare(x.RequestDate,y.RequestDate)},
{notifDate,(X,Y)=>的String.Compare(x.NotifDate,y.NotifDate)}
};
公开的IEnumerable< T>排序(IEnumerable的< T>清单,串HOWTO){
变种项= list.ToArray();
的Array.Sort(项,(X,Y)=>行动[HOWTO](X,Y));
返回的项目;
}
}
公共无效样品(){
VAR名单=新的名单,其中,YourDataClass>();
VAR分拣机=新的分拣< YourDataClass>();
VAR sortedItems = sorter.Sort(名单,reqDate);
}
I have the following right now:
switch (Mysort)
{
case "reqDate":
lstDMV.Sort((x, y) => DateTime.Compare(x.RequestDate, y.RequestDate));
break;
case "notifDate":
lstDMV.Sort((x, y) => DateTime.Compare(x.NotifDate, y.NotifDate));
break;
case "dueDate":
lstDMV.Sort((x, y) => String.Compare(x.TargetDateShort, y.TargetDateShort));
break;
case "days":
lstDMV.Sort((x, y) => x.DaysLapsed.CompareTo(y.DaysLapsed));
break;
}
I want to get rid of the case statement and just do something like:
lstDMV.Sort((x, y) => String.Compare(x.MySort, y.MySort));
The case statement is HUGE and it will really cut down on readability. But because MySort
is not contained in lstDMV
it's not working. Is there another way I can substitute it in?
I will of course change the text to make sure MySort
variable values match exactly to the lstDMV
property names.
i've also tried the following with no luck :(
if (sort != "")
{
string xsort, ysort;
xsort = "x." + sort;
ysort = "y." + sort;
lstDMV.Sort((x, y) => String.Compare(xsort, ysort));
}
解决方案
A dictionary with comparer Func's
public class YourDataClass {
public string RequestDate { get; set; }
public string NotifDate { get; set; }
.
.
.
}
public class Sorter<T> where T : YourDataClass {
private Dictionary<string, Func<T, T, int>> actions =
new Dictionary<string, Func<T, T, int>> {
{"reqDate", (x, y) => String.Compare(x.RequestDate, y.RequestDate)},
{"notifDate", (x, y) => String.Compare(x.NotifDate, y.NotifDate)}
};
public IEnumerable<T> Sort(IEnumerable<T> list, string howTo) {
var items = list.ToArray();
Array.Sort(items, (x, y) => actions[howTo](x, y));
return items;
}
}
public void Sample() {
var list = new List<YourDataClass>();
var sorter = new Sorter<YourDataClass>();
var sortedItems = sorter.Sort(list, "reqDate");
}
相关推荐
最新文章