Em cám ơn a nhiều. V khi em sort theo ID thì e phải thay String bằng gì ạ, e mới học c# nên còn lúng túng phần này quá…
Cách sử dụng Sort trong ArrayList trong C#?
ID của e kiểu dữ liệu là gì ??? String.Compare
là phương thức dùng để so sánh 2 chuỗi thôi mà.
Dạ e để ID là int và Balance e để là float. A cho e hỏi nếu để 2 kiểu đó thì mình phải thay String thành kiểu nào cho phù hợp ạ?
Với kiểu dữ liệu như vậy, thì e so sánh bình thường là được rồi mà.
Ví dụ :
int IComparer.Compare(object x, object y)
{
Account _acount1 = x as Account;
Account _acount2 = y as Account;
if(_acount1.ID>_acount2.ID)
return 1; //Lớn hơn
if(_acount1.ID>_acount2.ID)
return -1;//Nhở hơn
return 0;//Bằng nhau
}
Dạ, Tại e tưởng là mình dùng theo kiểu return String.Compare() giống trên luôn chứ k cầnn phải viết tường minh ra. Mà mình cho nó tự hiểu như String đc k a?
Mỗi kiểu dữ liệu có 1 cách so sánh riêng mà e.
Hoặc e có thể sử dụng với kiểu Interger như sau, còn kiểu float thì hình như k có
int IComparer.Compare(object x, object y)
{
Account _acount1 = x as Account;
Account _acount2 = y as Account;
return _acount1.ID.CompareTo(_acount2.ID);
}
Dạ. Em cám ơn anh nhiều
Anh ơi cho em hỏi trong một ArrayList em muốn gọi đến một chỉ số của ArrayList thì em phải làm sao a? Ví dụ em tìm người có mức lương cao nhất: Em gọi hàm sắp xếp từ tăng đến giảm. Sau đó, em chỉ in ra người đầu tiên trong danh sách v có được không anh?
E dùng List[0]
để lấy phần tử đầu tiên. Các phần tử trong Arraylist đánh số từ 0->n-1, với n là tổng số phần tử.
//Hàm để xuất Account
public void Report()
{
int i = 0;
foreach (Account acc in Accounts)
{
Console.WriteLine("STT: {0}", ++i);
acc.Query();
}
}
//Hàm xuất ArrayList
public void Report()
{
int i = 0;
foreach (Account acc in Accounts)
{
Console.WriteLine("STT: {0}", ++i);
acc.Query();
}
}
//Hàm tìm mức lương
public void TimMucLuongMax()
{
Accounts.Sort(new AccountBalanceComparer());
//Xuất ra phần tử đầu tiên tức mức lương lớn nhất
}
Nếu sử dụng List[0] e phải gọi như thế nào mới xuất ra được a???
E thử gọi : Accounts[0].Report()
xem đúng k
Dạ. Không đc a. Khi e thực hiện Accounts[0].(Equals, GetHashCode, GetType, ToString thôi a).
Hay tại em gọi nó ở trong class AccountList chứa Report() nên không thể thực hiện lệnh đc???
Ủa, e fai gọi nó ở hàm Main chứ nhỉ ???Sort cũng ở ngoài hàm Main chứ
Dạ k ý em là cài đặt hàm trong một class AccountList rồi sau đó em mới gọi trong hàm main đó a…
acc.TimMucLuongMax();
acc[0].Report();
Nếu e gọi như v trong hàm Main nó vẫn báo lỗi chỗ acc[0] a ơi…
E thử copy toàn bộ code của e lên đây đi e
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);
}
}
}
Dạ. Đúng đề yêu cầu r a. Cám ơn a nhiều nha