c#中实现自定义排序
通过实例说明:本实例包含3个文件,Person类,PersonName类,主程序
Person类:
代码
using System;
using System.Collections.Generic;
using System.Text;
namespace Sort
{
class Person:IComparable
{
public string Name;
public int Age;
public Person(string name,int age)
{
Name = name;
Age = age;
}
public int CompareTo(object obj)
{
if (obj is Person)
{
Person otherPerson = obj as Person;
return this.Age - otherPerson.Age;
}
else
{
throw new ArgumentException("object to compare to is not a person object");
}
}
}
}
using System.Collections.Generic;
using System.Text;
namespace Sort
{
class Person:IComparable
{
public string Name;
public int Age;
public Person(string name,int age)
{
Name = name;
Age = age;
}
public int CompareTo(object obj)
{
if (obj is Person)
{
Person otherPerson = obj as Person;
return this.Age - otherPerson.Age;
}
else
{
throw new ArgumentException("object to compare to is not a person object");
}
}
}
}
PersonName类:
代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace Sort
{
public class PersonCompareName:IComparer
{
public static IComparer Default = new PersonCompareName();
public int Compare(object x, object y)
{
if (x is Person && y is Person)
{
return Comparer.Default.Compare(((Person)x).Name, ((Person)y).Name);
}
else
{
throw new ArgumentException("One of the object to compare are not person obj");
}
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace Sort
{
public class PersonCompareName:IComparer
{
public static IComparer Default = new PersonCompareName();
public int Compare(object x, object y)
{
if (x is Person && y is Person)
{
return Comparer.Default.Compare(((Person)x).Name, ((Person)y).Name);
}
else
{
throw new ArgumentException("One of the object to compare are not person obj");
}
}
}
}
主程序调用:
代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace Sort
{
class Program
{
static void Main(string[] args)
{
ArrayList list = new ArrayList();
list.Add(new Person("tao", 23));
list.Add(new Person("hua", 22));
list.Add(new Person("qu", 21));
list.Add(new Person("zhan", 20));
Console.WriteLine("Unsort Person:");
for (int i = 0; i < list.Count;i++ )
{
Console.WriteLine("{0}---{1}", (list[i] as Person).Name, (list[i] as Person).Age);
}
Console.WriteLine();
Console.WriteLine("Sort Person by Age:");
list.Sort();
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine("{0}---{1}", (list[i] as Person).Name, (list[i] as Person).Age);
}
Console.WriteLine();
Console.WriteLine("Sort Person by Name:");
list.Sort(PersonCompareName.Default);
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine("{0}---{1}", (list[i] as Person).Name, (list[i] as Person).Age);
}
Console.WriteLine();
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace Sort
{
class Program
{
static void Main(string[] args)
{
ArrayList list = new ArrayList();
list.Add(new Person("tao", 23));
list.Add(new Person("hua", 22));
list.Add(new Person("qu", 21));
list.Add(new Person("zhan", 20));
Console.WriteLine("Unsort Person:");
for (int i = 0; i < list.Count;i++ )
{
Console.WriteLine("{0}---{1}", (list[i] as Person).Name, (list[i] as Person).Age);
}
Console.WriteLine();
Console.WriteLine("Sort Person by Age:");
list.Sort();
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine("{0}---{1}", (list[i] as Person).Name, (list[i] as Person).Age);
}
Console.WriteLine();
Console.WriteLine("Sort Person by Name:");
list.Sort(PersonCompareName.Default);
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine("{0}---{1}", (list[i] as Person).Name, (list[i] as Person).Age);
}
Console.WriteLine();
}
}
}
运行结果;


浙公网安备 33010602011771号