Seashorphius
Seashorphius

Reputation: 23

How to sort a variable in a separate method

I currently have this set up to display credit card inputs and outputs:

    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);
        }
    }

I have to create a separate method that needs to sort creditCard.CreditBalance from lowest to highest. So which would be the best way the sort the list by creditCard.CreditBalancewhich would then reflect the ViewReportthe next time the user opens it again?

Upvotes: 2

Views: 71

Answers (1)

Backs
Backs

Reputation: 24913

LINQ OrderBy:

foreach (PaymentPlan creditCard in paymentReport.Cast<PaymentPlan>().OrderBy(o=>o.CreditBalance))

To change order permanently assign result to your variable:

paymentReport = paymentReport.Cast<PaymentPlan>().OrderBy(o=>o.CreditBalance).ToArray();

Upvotes: 3

Related Questions