如何在单独的方法中对变量进行排序
本文关键字:变量 排序 方法 单独 | 更新日期: 2023-09-27 18:26:51
我目前有这个设置来显示信用卡输入和输出:
static void ViewReport(ArrayList paymentReport) //Displays the current payments in a list
{
//Activated if the user hits the v or V key
Console.WriteLine("'n{0,-5}{1,-25}{2,-15}{3,-15}{4,-15}", "Plan", "Number", "Balance", "Payment", "APR");
foreach (PaymentPlan creditCard in paymentReport)
{
Console.WriteLine("{0,-5}{1,-25}{2,-15}{3,-15}{4,-15}",
creditCard.PlanNumber,
creditCard.CardNumber,
creditCard.CreditBalance,
creditCard.MonthlyPayment,
creditCard.AnnualRate);
}
}
我必须创建一个单独的方法,它需要从最低到最高对creditCard.CreditBalance
进行排序。那么,按creditCard.CreditBalance
对列表进行排序的最佳方式是哪种,然后在用户下次再次打开时反映ViewReport
?
LINQ OrderBy:
foreach (PaymentPlan creditCard in paymentReport.Cast<PaymentPlan>().OrderBy(o=>o.CreditBalance))
要更改订单,请将结果永久分配给您的变量:
paymentReport = paymentReport.Cast<PaymentPlan>().OrderBy(o=>o.CreditBalance).ToArray();