本文主要是介绍利用ArrayList类编写客户信息管理程序,利用Comparer接口实现自定义Sort(IComparer comparer)排序,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.利用数组列表的常用成员,实现客户信息类内部的人员信息添加,删除,显示等功能
2.通过将客户信息类(CustomerInfo)继承实现接口IComparable成员,实现客户信息类的对象Sort()功能
3.通过自定义类实现IComparer接口成员,实现按照特定信息对客户信息的对象集合进行排序
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace MyCompareInterfaceDemo
{/// <summary>/// //定义一个客户信息类/// </summary>public class CustomerInfo:IComparable{public string Name { get; set; }public int ID { get; set; }public string CompanyName { get; set; }public CustomerInfo(string name,int ID,string name_company){Name = name;this.ID = ID;CompanyName = name_company;}public CustomerInfo() { }/// <summary>/// //定义一个私有的客户信息列表/// </summary>static ArrayList customers = new ArrayList();/// <summary>/// //定义一个公共方法Add,逐个添加客户信息/// </summary>/// <param name="cust"></param>public void AddCustomer(CustomerInfo cust){customers.Add(cust);}/// <summary>/// //定义一个公共方法,添加一组客户信息/// </summary>/// <param name="custs"></param>public void AddCustomers(params CustomerInfo[] custs){customers.AddRange(custs);}//定义一个公共方法Remove,删除指定的客户信息public void RemoveCustomer(CustomerInfo cust){int index_cust = customers.IndexOf(cust);if (index_cust>0){customers.RemoveAt(index_cust);}}public void ShowInfo(){foreach (CustomerInfo item in customers){Console.WriteLine($"{item.ID},{item.Name},{item.CompanyName}");}Console.ReadKey();}//将Arraylist按照指定的方式排序public void SortByName(){//public virtual void Sort(IComparer comparer);//Sort方法的参数是IComparer对象,需要自己写一个比较类,实现IComaprer接口作为Sort参数customers.Sort(new MyCompareClass());}public void SortbyID(){//Sort方法对集合进行排序,必须确保集合的每个元素类型都实现了IComparable接口//这里集合的元素类型是类CustomerInfo,所以要在这个类种实现IComparable接口//1.类CustomerInfo继承IComparable接口;2.CustomerInfo类中实现IComparable接口的成员customers.Sort();}/// <summary>/// 升序排列/// </summary>/// <param name="obj"></param>/// <returns></returns>public int CompareTo (object obj) => this.ID.CompareTo((obj as CustomerInfo).ID);/// <summary>/// 降序排列/// </summary>/// <param name="obj"></param>/// <returns></returns>//public int CompareTo(object obj) => (obj as CustomerInfo).ID.CompareTo(this.ID);}/// <summary>/// 自定义一个Compare类,实现IComparer接口,即实现该接口的Compare方法int Compare(object x, object y)/// </summary>public class MyCompareClass:IComparer{//public interface IComparer//{// int Compare(object x, object y);//}public int Compare(object cust1,object cust2){return (cust1 as CustomerInfo).Name.CompareTo((cust2 as CustomerInfo).Name);}}
}
在Main()方法中实例化若干个客户对象,添加到集合中,并通过默认的Sort()方法和自定义的Sort(IComparer comparer)方法,实现客户信息的排序,如下代码:
class Program{static void Main(string[] args){CustomerInfo cust1 = new CustomerInfo("XiaoSheng", 1, "ASUS");CustomerInfo cust2 = new CustomerInfo("DaWang", 2, "CURTIS");CustomerInfo cust3 = new CustomerInfo("XiaoWang", 3, "SND YIZHONG");CustomerInfo cust4 = new CustomerInfo { Name = "ZhangSan", ID = 4, CompanyName = "ASDF" };CustomerInfo cust = new CustomerInfo();cust.AddCustomer(cust2);cust.AddCustomer(cust4);cust.AddCustomers(cust3, cust1);cust.ShowInfo();cust.RemoveCustomer(cust4);cust.ShowInfo();//cust.SortByName();cust.SortbyID();cust.ShowInfo();}}
客户集合默认顺序如下:
2,DaWang,CURTIS
4,ZhangSan,ASDF
3,XiaoWang,SND YIZHONG
1,XiaoSheng,ASUS
删除ID=4的客户后,显示如下:
2,DaWang,CURTIS
3,XiaoWang,SND YIZHONG
1,XiaoSheng,ASUS
按照ID排序后显示如下:
1,XiaoSheng,ASUS
2,DaWang,CURTIS
3,XiaoWang,SND YIZHONG
按照name排序后显示如下:
2,DaWang,CURTIS
1,XiaoSheng,ASUS
3,XiaoWang,SND YIZHONG
这篇关于利用ArrayList类编写客户信息管理程序,利用Comparer接口实现自定义Sort(IComparer comparer)排序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!