Cách sử dụng Sort trong ArrayList trong C#?

public class Account
{
    private int accountID;
    private string firstName;
    private string lastName;
    private decimal balance;

    //Implement IComparable interface
    public int CompareTo(object obj)
    {
        if(obj is Account)
        {
            return this.accountID.CompareTo((obj as Account).accountID);
        }

        throw new ArgumentException("Object is not a User");
    }

    public void FillInfo()
    {
        Console.WriteLine("----------------------------------------------------------------------------");
        Console.Write("Account ID: ");
        accountID = int.Parse(Console.ReadLine());
        Console.Write("First Name: ");
        firstName = Console.ReadLine();
        Console.Write("Last Name: ");
        lastName = Console.ReadLine();
        Console.Write("Balance: ");
        balance = int.Parse(Console.ReadLine());
        Console.WriteLine("----------------------------------------------------------------------------");
    }

    public void Query()
    {
        Console.WriteLine("----------------------------------------------------------------------------");
        Console.WriteLine("Account ID: {0}", this.accountID);
        Console.WriteLine("First Name: {0}", this.firstName);
        Console.WriteLine("Last Name: {0}", this.lastName);
        Console.WriteLine("Balance: {0}", this.balance);
        Console.WriteLine("----------------------------------------------------------------------------");
    }

}


//------------------------------------------------- CLASS ACCOUNT LIST --------------------------------------------------------


public class AccountList
{
    private ArrayList Accounts = new ArrayList();

    public ArrayList ACCOUNTS
    {
        get { return Accounts; }
        set { Accounts = value; }
    }


    //-------------------------------------1.Add -------------------------------------
    public void NewAccount()
    {

        Account acc = new Account();
        Console.WriteLine("NHAP THONG TIN NGUOI DUNG");
        acc.FillInfo();
        Accounts.Add(acc);
    }


    //-------------------------------------2. Xuất  -------------------------------------
    public void Report()
    {
        int i = 0;
        foreach (Account acc in Accounts)
        {
            Console.WriteLine("STT: {0}", ++i);
            acc.Query();
        }
    }

    //------------------------------------------8.Sắp xếp theo thứ tự tăng dần của Balance----------------------
    public void SapXepTangLuong()
    {
        Console.WriteLine("\nSap xep tang theo Luong");
        Accounts.Sort(new AccountBalanceComparer());
        Report();
    }

    //------------------------------------------9. Tìm người có mức lương cao nhất----------------------
    public void TimMucLuongMax()
    {
        Accounts.Sort(new AccountBalanceComparer());
    }

    //So sánh theo lương giảm dần
    public class BanlanceComparer:IComparer
    {
        int IComparer.Compare(object x, object y)
        {
            Account acc1 = x as Account;
            Account acc2 = y as Account;
            if (acc1.Balance > acc2.Balance)
                return -1;
            if (acc1.Balance < acc2.Balance)
                return 1;
            return 0;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            AccountList acc = new AccountList();

            int c;
            do
            {
                Console.WriteLine("\t1. Add");
                Console.WriteLine("\t2. Save");
                Console.WriteLine("\t3. Load");
                Console.WriteLine("\t4. Report");
                Console.WriteLine("\t5. Remove");
                Console.WriteLine("\t6. Sap xep tang dan theo ID");
                Console.WriteLine("\t7. Sap xep tang dan theo ten");
                Console.WriteLine("\t8. Sap xep tang theo luong");
                Console.WriteLine("\t9. Tim nguoi co muc luong cao nhat");
                Console.WriteLine("\t0. Exit");

                Console.Write("\n\nNhap vao lua chon: ");
                c = int.Parse(Console.ReadLine());
                switch (c)
                {
                    case 9:
                        {
                            acc.TimMucLuongMax();

                            break;
                        }
                    case 0:
                        {
                            break;
                        }
                }
            } while (c != 0);
        }
    }
}

Code của e đó a.

Code e copy lên sao lỗi tùm lum zị e ? a sửa lại, k biết đúng ý e k.

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace ConsoleApplication1
{
    public class Account
    {
        private int accountID;
        private string firstName;
        private string lastName;
        public decimal balance;

        //Implement IComparable interface
        public int CompareTo(object obj)
        {
            if (obj is Account)
            {
                return this.accountID.CompareTo((obj as Account).accountID);
            }

            throw new ArgumentException("Object is not a User");
        }

        public void FillInfo()
        {
            Console.WriteLine("----------------------------------------------------------------------------");
            Console.Write("Account ID: ");
            accountID = int.Parse(Console.ReadLine());
            Console.Write("First Name: ");
            firstName = Console.ReadLine();
            Console.Write("Last Name: ");
            lastName = Console.ReadLine();
            Console.Write("Balance: ");
            balance = int.Parse(Console.ReadLine());
            Console.WriteLine("----------------------------------------------------------------------------");
        }

        public void Query()
        {
            Console.WriteLine("----------------------------------------------------------------------------");
            Console.WriteLine("Account ID: {0}", this.accountID);
            Console.WriteLine("First Name: {0}", this.firstName);
            Console.WriteLine("Last Name: {0}", this.lastName);
            Console.WriteLine("Balance: {0}", this.balance);
            Console.WriteLine("----------------------------------------------------------------------------");
        }

    }


    //------------------------------------------------- CLASS ACCOUNT LIST --------------------------------------------------------

    public class AccountList
    {
        private ArrayList Accounts = new ArrayList();

        public ArrayList ACCOUNTS
        {
            get { return Accounts; }
            set { Accounts = value; }
        }


        //-------------------------------------1.Add -------------------------------------
        public void NewAccount()
        {

            Account acc = new Account();
            Console.WriteLine("NHAP THONG TIN NGUOI DUNG");
            acc.FillInfo();
            Accounts.Add(acc);
        }


        //-------------------------------------2. Xuất  -------------------------------------
        public void Report()
        {
            int i = 0;
            foreach (Account acc in Accounts)
            {
                Console.WriteLine("STT: {0}", ++i);
                acc.Query();
            }
        }

        //------------------------------------------8.Sắp xếp theo thứ tự tăng dần của Balance----------------------
        public void SapXepTangLuong()
        {
            Console.WriteLine("\nSap xep tang theo Luong");
            Accounts.Sort(new BanlanceComparer());
            Report();
        }

        //------------------------------------------9. Tìm người có mức lương cao nhất----------------------
        public void TimMucLuongMax()
        {
            Accounts.Sort(new BanlanceComparer());
            Account Max = Accounts[0] as Account;
            if (Max != null)
            {
                Max.Query();
            }
           
        }


        //So sánh theo lương giảm dần
        public class BanlanceComparer : IComparer
        {
            int IComparer.Compare(object x, object y)
            {
                Account acc1 = x as Account;
                Account acc2 = y as Account;
                if (acc1.balance > acc2.balance)
                    return -1;
                if (acc1.balance < acc2.balance)
                    return 1;
                return 0;
            }
        }

    }

    class Program
    {
        static void Main(string[] args)
        {
            AccountList acc = new AccountList();

            int c;
            do
            {
                Console.WriteLine("\t1. Add");
                Console.WriteLine("\t2. Save");
                Console.WriteLine("\t3. Load");
                Console.WriteLine("\t4. Report");
                Console.WriteLine("\t5. Remove");
                Console.WriteLine("\t6. Sap xep tang dan theo ID");
                Console.WriteLine("\t7. Sap xep tang dan theo ten");
                Console.WriteLine("\t8. Sap xep tang theo luong");
                Console.WriteLine("\t9. Tim nguoi co muc luong cao nhat");
                Console.WriteLine("\t0. Exit");

                Console.Write("\n\nNhap vao lua chon: ");
                c = int.Parse(Console.ReadLine());
                switch (c)
                {
                    case 9:
                        {
                            acc.TimMucLuongMax();

                            break;
                        }
                    case 0:
                        {
                            break;
                        }
                }
            } while (c != 0);
        }
    }
}

2 Likes

Dạ. Đúng đề yêu cầu r a. Cám ơn a nhiều nha :smiley:

A post was split to a new topic: Hiển thị dữ liệu lên DataGridView

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?