这是一个示例代码,我希望以这种方式按照降序排列TotalCharge属性的排序数据。
//List<RateStatus> SortedList = objListOrder.OrderByDescending(o=>o.TotalCharge).ToList();
请参阅下面的代码并指导我如何在desending中对TotalCharge属性进行排序。感谢
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
List<RateStatus> oRateCollection = new List<RateStatus>();
RateStatus oRateStatus = new RateStatus();
oRateStatus.StatusMsg = "Ok";
oRateStatus.ErrorMsg = "None";
oRateStatus.RateDetails.Add(new RateDetails()
{
CurrentcyCode = "GBP",
TotalCharge = 20
});
oRateCollection.Add(oRateStatus);
// i want something like this below
//List<RateStatus> SortedList = objListOrder.OrderByDescending(o=>o.TotalCharge).ToList();
}
}
public class RateStatus
{
public RateStatus()
{
RateDetails = new List<RateDetails>();
}
public string StatusMsg
{
get;
set;
}
public string ErrorMsg
{
get;
set;
}
public List<RateDetails> RateDetails
{
get;
set;
}
}
public class RateDetails
{
public string ServiceCode
{
get;
set;
}
public string ServiceName
{
get;
set;
}
public string CurrentcyCode
{
get;
set;
}
public double TotalCharge
{
get;
set;
}
}
oRateStatus.RateDetails.Sort(new RateStatusComparer());
oRateCollection.Add(oRateStatus);
public class RateStatusComparer : IComparer<RateDetails>
{
public int Compare(RateDetails x, RateDetails y)
{
if (x.TotalCharge > y.TotalCharge) return -1;
else if (x.TotalCharge < y.TotalCharge) return 1;
else return 0;
}
}
感谢
答案 0 :(得分:0)
以下是代码
由于总费用是一个数值,我们可以使用Min和Max方法进行排序。
降序:
List<RateStatus> SortedList = oRateCollection.OrderByDescending(o => o.RateDetails.Max(r => r.TotalCharge)).ToList();
答案 1 :(得分:0)
创建一个实现IComparer<RateStatus>
的类。
例如,像这样:
public class RateStatusComparer: IComparer<RateStatus>
{
public int Compare(RateStatus x, RateStatus y)
{
... your comparing code here
}
}
然后你可以使用List.Sort
的重载并提供你的比较器
var с = new RateStatusComparer();
objListOrder.Sort(c);
有关详细信息,请参阅MSDN。