您的位置:首页 > 编程语言 > C#

c#:sort在非泛型类ArrayList,泛型类List<>中的用法

2018-01-02 15:09 429 查看
例1、string数组排序

static void Main(string[] args)
{
string[] s = { "a", "A", "-1", "1a", "2B", "AB", "b", "B" };

//Array.Sort(s, (a, b) => string.Compare(a, b, StringComparison.OrdinalIgnoreCase));   //匿名函数lamb表达式
Array.Sort(s, new Comparison<string>(comp));

foreach (object a in s)
{
Console.WriteLine(a);
}
Console.Read();
}

public static int comp(string m, string n)
{
return string.Compare(m, n, StringComparison.OrdinalIgnoreCase);
}

例2、ArrayList排序

1>基础类

class Program
{
static void Main(string[] args)
{
ArrayList list = new ArrayList(new string[9] { "c", "a", "b", "B", "A", "C", "1", "2", "3" });

IComparer myComparer = new myClass();
list.Sort(myComparer);

foreach (object a in list)
{
Console.WriteLine(a);
}
Console.Read();
}
}

public class myClass : IComparer
{
int IComparer.Compare(Object x, Object y)
{
return Comparer.Default.Compare(x, y);

}
}

2>自定义类:非泛型集合类

i)

using System.Collections;
public class Person
{
private string name;
private int age;

public string Name
{
get { return name; }
set { name = value; }
}

public int Age
{
get { return age; }
set { age = value; }
}

public Person(string name, int age)
{
this.name = name;
this.age = age;
}
}
class Program
{
static void Main(string[] args)
{
//Person p1 = new Person("c", 3);
//Person p2 = new Person("b", 4);
//Person p3 = new Person("s", 5);
//ArrayList list = new ArrayList();
//list.Add(p1);
//list.Add(p2);
//list.Add(p3);

Person[] persons = new Person[3];
persons[0] = new Person("c", 10);
persons[1] = new Person("b", 4);
persons[2] = new Person("s", 5);

ArrayList lists = new ArrayList();
lists.AddRange(persons);

foreach (Person P in lists)
{
Console.WriteLine("{0},{1}", P.Name, P.Age);
}

IComparer myComparer = new myClass();
lists.Sort(myComparer);

foreach (Person P in lists)
{
Console.WriteLine("{0},{1}", P.Name, P.Age);
}
}
}

public class myClass : IComparer
{
int IComparer.Compare(Object x, Object y)
{
Person personX = (Person)x;
Person personY = (Person)y;

//if (personX.Age > personY.Age)
//    return 1;
//if (personX.Age < personY.Age)
//    return -1;
//else
//    return 0;
return personX.Age.CompareTo(personY.Age);       //按年龄比较
//return  personX.Name.CompareTo(personY.Name);  //按名字比较
}
}
ii)
public class CustomerInfo
{
private static ArrayList CustomerList = new ArrayList();
private string id;
private string name;
private string address;

public string ID
{
get { return id; }
set { id = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}

public string Address
{
get { return address; }
set { address = value; }
}

public CustomerInfo()
{

}
public CustomerInfo(string id, string name, string address)
{
this.id = id;
this.name = name;
this.address = address;
}

public static void AddCustomer(CustomerInfo aCustomerInfo)
{
CustomerList.Add(aCustomerInfo);
}

public static void Delete(CustomerInfo oo)
{
int i = CustomerList.IndexOf(oo);
if (i < 0)
{
Console.WriteLine("no!");
}
else
{
CustomerList.RemoveAt(i);
}
}

public static void Show()
{
foreach (CustomerInfo s in CustomerList)
{
Console.WriteLine(s.ID + ",  " + s.Name + ",  " + s.Address);
}
}

public static void SortByName()   //通过接口实现按照客户姓名排序的方法
{
CustomerList.Sort(new CustomerNameCompare());  //实现自定义排序的接口
}

public static void SortByID()   //通过接口实现按照客户ID排序的方法
{
CustomerList.Sort(new CustomerIdCompare());  //实现自定义排序的接口
}

public static void DeleteByName(string name)
{
for (int i = 0; i < CustomerList.Count; i++)
{
CustomerInfo aCustomerInfo = (CustomerInfo)CustomerList[i];
if (aCustomerInfo.Name == name)
{
//CustomerInfo.Delete(aCustomerInfo);
CustomerList.RemoveAt(i);
break;
}
}
}
}

public class CustomerNameCompare : IComparer    //按名字排序
{
public int Compare(object x, object y)
{
//CustomerInfo customerInfoX = (CustomerInfo)x;
//CustomerInfo customerInfoY = (CustomerInfo)y;
//return new CaseInsensitiveComparer().Compare(customerInfoX.Name, customerInfoY.Name);

return new CaseInsensitiveComparer().Compare(((CustomerInfo)x).Name, ((CustomerInfo)y).Name);     //CaseInsensitiveComparer类:比较两个对象相等,忽略大小写的字符串。
}
}

public class CustomerIdCompare : IComparer    //按ID排序
{
public int Compare(object x, object y)
{
return new CaseInsensitiveComparer().Compare(((CustomerInfo)x).ID, ((CustomerInfo)y).ID);
}
}
static void Main(string[] args)
{
CustomerInfo a1CustomerInfo = new CustomerInfo("Id001", "张三", "河南郑州");
CustomerInfo.AddCustomer(a1CustomerInfo);
CustomerInfo a2CustomerInfo = new CustomerInfo("Id002", "李四", "陕西安康");
CustomerInfo.AddCustomer(a2CustomerInfo);
CustomerInfo a3CustomerInfo = new CustomerInfo("Id003", "王五", "河南开封");
CustomerInfo.AddCustomer(a3CustomerInfo);

Console.WriteLine("排序前的集合排列");
CustomerInfo.Show();
CustomerInfo.SortByName();
Console.WriteLine("按姓名排序后的集合排列");
CustomerInfo.Show();
CustomerInfo.SortByID();
Console.WriteLine("按ID排序后的集合排列");
CustomerInfo.Show();

CustomerInfo a4CustomerInfo = new CustomerInfo("Id004", "赵七", "陕西西安");
CustomerInfo.AddCustomer(a4CustomerInfo);
Console.WriteLine("添加一个客户后的所有信息");
CustomerInfo.Show();
CustomerInfo.Delete(a4CustomerInfo);
Console.WriteLine("删除一个客户对象后的所有信息");
CustomerInfo.Show();

CustomerInfo.DeleteByName("张三");
Console.WriteLine("删除张三客户后的所有信息");
CustomerInfo.Show();

Console.Read();
}

3>自定义类:泛型集合类

public class CustomerInfo
{
private static List<CustomerInfo> CustomerList = new List<CustomerInfo>();
private string id;
private string name;
private string address;

public string ID
{
get { return id; }
set { id = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}

public string Address
{
get { return address; }
set { address = value; }
}

public CustomerInfo()
{

}
public CustomerInfo(string id, string name, string address)
{
this.id = id;
this.name = name;
this.address = address;
}

public static void AddCustomer(CustomerInfo aCustomerInfo)
{
CustomerList.Add(aCustomerInfo);
}

public static void Delete(CustomerInfo oo)
{
int i = CustomerList.IndexOf(oo);
if (i < 0)
{
Console.WriteLine("no!");
}
else
{
CustomerList.RemoveAt(i);
}
}

public static void Show()
{
foreach (CustomerInfo s in CustomerList)
{
Console.WriteLine(s.ID + ",  " + s.Name + ",  " + s.Address);
}
}

public static void SortByName()   //通过接口实现按照客户姓名排序的方法
{
CustomerList.Sort(new CustomerNameCompare());  //实现自定义排序的接口
}

public static void SortByID()   //通过接口实现按照客户ID排序的方法
{
CustomerList.Sort(new CustomerIdCompare());  //实现自定义排序的接口
}

public static void DeleteByName(string name)
{
for (int i = 0; i < CustomerList.Count; i++)
{
CustomerInfo aCustomerInfo = (CustomerInfo)CustomerList[i];
if (aCustomerInfo.Name == name)
{
CustomerList.RemoveAt(i);
break;
}
}
}
}

public class CustomerNameCompare : IComparer<CustomerInfo>    //按名字排序
{
public int Compare(CustomerInfo x, CustomerInfo y)   //有object类改为具体类CustomerInfo
{
//return new CaseInsensitiveComparer().Compare(x.Name, y.Name);     //CaseInsensitiveComparer类:比较两个对象相等,忽略大小写的字符串。
//return x.Name.CompareTo(y.Name);     //按名字升序排列
return y.Name.CompareTo(x.Name);       //按名字降序排列
}
}

public class CustomerIdCompare : IComparer<CustomerInfo>    //按ID排序
{
public int Compare(CustomerInfo x, CustomerInfo y)                  ////有object类改为具体类CustomerInfo
{
//return new CaseInsensitiveComparer().Compare(x.ID, y.ID);
//return x.ID.CompareTo(y.ID);            //按ID升序排列
return y.ID.CompareTo(x.ID);              //按ID升序排列
}
}





static void Main(string[] args)
{
CustomerInfo a1CustomerInfo = new CustomerInfo("Id001", "张三", "河南郑州");
Cust
da34
omerInfo.AddCustomer(a1CustomerInfo);
CustomerInfo a2CustomerInfo = new CustomerInfo("Id002", "李四", "陕西安康");
CustomerInfo.AddCustomer(a2CustomerInfo);
CustomerInfo a3CustomerInfo = new CustomerInfo("Id003", "王五", "河南开封");
CustomerInfo.AddCustomer(a3CustomerInfo);

Console.WriteLine("排序前的集合排列");
CustomerInfo.Show();
CustomerInfo.SortByName();
Console.WriteLine("按姓名排序后的集合排列");
CustomerInfo.Show();
CustomerInfo.SortByID();
Console.WriteLine("按ID排序后的集合排列");
CustomerInfo.Show();

CustomerInfo a4CustomerInfo = new CustomerInfo("Id004", "赵七", "陕西西安");
CustomerInfo.AddCustomer(a4CustomerInfo);
Console.WriteLine("添加一个客户后的所有信息");
CustomerInfo.Show();
CustomerInfo.Delete(a4CustomerInfo);
Console.WriteLine("删除一个客户对象后的所有信息");
CustomerInfo.Show();

CustomerInfo.DeleteByName("张三");
Console.WriteLine("删除张三客户后的所有信息");
CustomerInfo.Show();

Console.Read();
}


运行结果同2>ii。

升序:

  降序:


注:在Compare方法中降序排列使用y.CompareTo(x)写法,升序排列使用x.CompareTo(y)写法实现。

4>IComparable<>用法

public class CustomerInfo : IComparable<CustomerInfo>
{
private static List<CustomerInfo> CustomerList = new List<CustomerInfo>();
private string id;
private string name;
private string address;

public string ID
{
get { return id; }
set { id = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}

public string Address
{
get { return address; }
set { address = value; }
}

public CustomerInfo()
{

}
public CustomerInfo(string id, string name, string address)
{
this.id = id;
this.name = name;
this.address = address;
}

public int CompareTo(CustomerInfo objCustomer)
{
return this.Name.CompareTo(objCustomer.Name); //返回按照姓名比较的结果,string类型实现了CompareTo()函数.
//return this.ID.CompareTo(objCustomer.ID); //返回按照ID比较的结果,string类型实现了CompareTo()函数.
}

public static void AddCustomer(CustomerInfo aCustomerInfo)
{
CustomerList.Add(aCustomerInfo);
}

public static void Delete(CustomerInfo oo)
{
int i = CustomerList.IndexOf(oo);
if (i < 0)
{
Console.WriteLine("no!");
}
else
{
CustomerList.RemoveAt(i);
}
}

public static void Show()
{
foreach (CustomerInfo s in CustomerList)
{
Console.WriteLine(s.ID + ", " + s.Name + ", " + s.Address);
}
}

public static void SortByName() //通过接口实现按照客户姓名排序的方法
{
CustomerList.Sort(new CustomerNameCompare()); //实现自定义排序的接口
}

public static void SortByID() //通过接口实现按照客户ID排序的方法
{
CustomerList.Sort(new CustomerIdCompare()); //实现自定义排序的接口
}

public static void DeleteByName(string name)
{
for (int i = 0; i < CustomerList.Count; i++)
{
CustomerInfo aCustomerInfo = (CustomerInfo)CustomerList[i];
if (aCustomerInfo.Name == name)
{
CustomerList.RemoveAt(i);
break;
}
}
}
}

public class CustomerNameCompare : IComparer<CustomerInfo> //按名字排序
{
public int Compare(CustomerInfo x, CustomerInfo y) //有object类改为具体类CustomerInfo
{
//return new CaseInsensitiveComparer().Compare(x.Name, y.Name); //CaseInsensitiveComparer类:比较两个对象相等,忽略大小写的字符串。
//return x.Name.CompareTo(y.Name); //按名字升序排列
//return y.Name.CompareTo(x.Name); //按名字降序排列
return x.CompareTo(y); //该函数确定排序方法,要么按名字,要么按ID,二选一。 程序中该函数按姓名排序。
}
}

public class CustomerIdCompare : IComparer<CustomerInfo> //按ID排序
{
public int Compare(CustomerInfo x, CustomerInfo y) ////有object类改为具体类CustomerInfo
{
//return new CaseInsensitiveComparer().Compare(x.ID, y.ID);
//return x.ID.CompareTo(y.ID); //按ID升序排列
//return y.ID.CompareTo(x.ID); //按ID升序排列
return x.CompareTo(y); //该函数确定排序方法,要么按名字,要么按ID,二选一。程序中该函数按姓名排序。
}
}
static void Main(string[] args)
{
CustomerInfo a1CustomerInfo = new CustomerInfo("Id001", "张三", "河南郑州");
CustomerInfo.AddCustomer(a1CustomerInfo);
CustomerInfo a2CustomerInfo = new CustomerInfo("Id002", "李四", "陕西安康");
CustomerInfo.AddCustomer(a2CustomerInfo);
CustomerInfo a3CustomerInfo = new CustomerInfo("Id003", "王五", "河南开封");
CustomerInfo.AddCustomer(a3CustomerInfo);

if (a1CustomerInfo.CompareTo(a2CustomerInfo) > 0)
{
Console.WriteLine("{0}的姓名比{1}的姓名排列靠前",a1CustomerInfo.Name,a2CustomerInfo.Name);
}
else
{
Console.WriteLine("{0}的姓名比{1}的姓名排列靠后", a1CustomerInfo.Name, a2CustomerInfo.Name);
}

Console.WriteLine("排序前的集合排列");
CustomerInfo.Show();
CustomerInfo.SortByName();      //程序中按姓名排序。
Console.WriteLine("按姓名排序后的集合排列");
CustomerInfo.Show();
CustomerInfo.SortByID();        //调用该函数实际是按姓名排序。
Console.WriteLine("按ID排序后的集合排列");
CustomerInfo.Show();

CustomerInfo a4CustomerInfo = new CustomerInfo("Id004", "赵七", "陕西西安");
CustomerInfo.AddCustomer(a4CustomerInfo);
Console.WriteLine("添加一个客户后的所有信息");
CustomerInfo.Show();
CustomerInfo.Delete(a4CustomerInfo);
Console.WriteLine("删除一个客户对象后的所有信息");
CustomerInfo.Show();

CustomerInfo.DeleteByName("张三");
Console.WriteLine("删除张三客户后的所有信息");
CustomerInfo.Show();

Console.Read();
}
运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: