添加插入排序,按天平排序。还为校验位创建循环

时间:2017-05-02 20:35:08

标签: c#

截至目前,我已经制作了多个帐户。帐号和余额。我想添加一个插入排序,按降序对余额进行排序。

所以简而言之,我需要一些帮助按帐户余额排序 - 这是代码

class Program
{
    public class Account
    {
        public string AccountName { get; set; }
        public int AccountId { get; set; }
        public double AccountBal { get; set; }
        public override string ToString()
        {
            return "ID: " + AccountId + "   Name: " + AccountName + " \tBalance:  " + String.Format("{0:c}", AccountBal);
        }
        static void Main(string[] args)
        {
            List<string> actNames = new List<string>
            { "Bill","Laura","John","Joe","Chris","Pete","Sally","Jessica","Paul","Lynsey"};
            Random rnd = new Random();
            int i, tmp, idx;
            int n = actNames.Count;
            double total = 0;
            double actBal = 0;
            double avg = 0;
            List<Account> customer = new List<Account>();
            for (i = 0; i < n; i++)
            {
                idx = rnd.Next(0, actNames.Count);
                Account act = new Account();
                act.AccountName = actNames[idx]; // Account Name
                actNames.RemoveAt(idx);
                tmp = rnd.Next(10000, 99999);
                tmp += tmp * 100 + (tmp / 100000) + (tmp % 100000) / 1000 + tmp;
                act.AccountId = tmp; // Account ID #
                customer.Add(act);
                actBal = rnd.Next(100, 6000);
                act.AccountBal = actBal; // Account Balance
                total += actBal; // sum the account balances in the loop
            }
            foreach (Account aID in customer)
            {
                Console.WriteLine(aID);
            }

            Console.WriteLine();
            avg = total / 10; // avg outside the loop
            Console.WriteLine("\nThe average account balance is: {0:C}", avg);
            Console.ReadLine();


        }
    }
}

}

1 个答案:

答案 0 :(得分:0)

使用链接非常容易

YourList.OrderByDescending(Acc=>Acc.Balance);