自定义对象的比较系列一之实现IComparable
2009-05-07 11:03 Virus-BeautyCode 阅读(502) 评论(0) 编辑 收藏 举报
要进行比较的自定义对象
MemberEntity
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class MemberEntity:IComparable
{
#region
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
private int _age;
public int Age
{
get { return _age; }
set { _age = value; }
}
private DateTime _birth;
public DateTime Birth
{
get { return _birth; }
set { _birth = value; }
}
#endregion
public int CompareTo(object obj)
{
MemberEntity member = obj as MemberEntity;
return string.Compare(this._name, member._name);
}
private class sortByAgeAscHelper : IComparer
{
int IComparer.Compare(object a, object b)
{
MemberEntity m1 = a as MemberEntity;
MemberEntity m2 = b as MemberEntity;
if (m1._age > m2._age)
return 1;
if (m1._age < m2._age)
return -1;
else
return 0;
}
}
private class sortByAgeDescHelper : IComparer
{
int IComparer.Compare(object a, object b)
{
MemberEntity m1 = a as MemberEntity;
MemberEntity m2 = b as MemberEntity;
if (m1._age < m2._age)
return 1;
if (m1._age> m2._age)
return -1;
else
return 0;
}
}
private class sortByNameDescHelper : IComparer
{
int IComparer.Compare(object a, object b)
{
MemberEntity m1 = a as MemberEntity;
MemberEntity m2 = b as MemberEntity;
return string.Compare(m2._name, m1._name);
}
}
public static IComparer sortByAgeAsc()
{
return (IComparer)new sortByAgeAscHelper();
}
public static IComparer sortByAgeDesc()
{
return (IComparer)new sortByAgeDescHelper();
}
public static IComparer sortByNameDesc()
{
return (IComparer)new sortByNameDescHelper();
}
}
}
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class MemberEntity:IComparable
{
#region
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
private int _age;
public int Age
{
get { return _age; }
set { _age = value; }
}
private DateTime _birth;
public DateTime Birth
{
get { return _birth; }
set { _birth = value; }
}
#endregion
public int CompareTo(object obj)
{
MemberEntity member = obj as MemberEntity;
return string.Compare(this._name, member._name);
}
private class sortByAgeAscHelper : IComparer
{
int IComparer.Compare(object a, object b)
{
MemberEntity m1 = a as MemberEntity;
MemberEntity m2 = b as MemberEntity;
if (m1._age > m2._age)
return 1;
if (m1._age < m2._age)
return -1;
else
return 0;
}
}
private class sortByAgeDescHelper : IComparer
{
int IComparer.Compare(object a, object b)
{
MemberEntity m1 = a as MemberEntity;
MemberEntity m2 = b as MemberEntity;
if (m1._age < m2._age)
return 1;
if (m1._age> m2._age)
return -1;
else
return 0;
}
}
private class sortByNameDescHelper : IComparer
{
int IComparer.Compare(object a, object b)
{
MemberEntity m1 = a as MemberEntity;
MemberEntity m2 = b as MemberEntity;
return string.Compare(m2._name, m1._name);
}
}
public static IComparer sortByAgeAsc()
{
return (IComparer)new sortByAgeAscHelper();
}
public static IComparer sortByAgeDesc()
{
return (IComparer)new sortByAgeDescHelper();
}
public static IComparer sortByNameDesc()
{
return (IComparer)new sortByNameDescHelper();
}
}
}
比较测试代码
比较测试代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
using System.Text;
using System.Data;
using System.IO;
using System.Xml.XPath;
using System.Xml;
namespace ConsoleApplication1
{
class Program
{
ArrayList memberList = new ArrayList ()
{
new MemberEntity(){ Name="aaa", Age =12},
new MemberEntity(){ Name="ccc" ,Age=14},
new MemberEntity(){ Name="bbb", Age=15}
};
Console.WriteLine("List -UnSorted\n");
foreach (MemberEntity m in memberList)
{
Console.WriteLine(m.Name +"\t\t"+m.Age.ToString());
}
memberList.Sort();
Console.WriteLine("List -Sorted by Name Asc");
foreach (MemberEntity m in memberList)
{
Console.WriteLine(m.Name + "\t\t" + m.Age.ToString());
}
memberList.Sort(MemberEntity.sortByNameDesc());
Console.WriteLine("List -Sorted by Name Desc");
foreach (MemberEntity m in memberList)
{
Console.WriteLine(m.Name + "\t\t" + m.Age.ToString());
}
memberList.Sort(MemberEntity.sortByAgeAsc ());
Console.WriteLine("List -Sorted by Age Asc");
foreach (MemberEntity m in memberList)
{
Console.WriteLine(m.Name + "\t\t" + m.Age.ToString());
}
memberList.Sort(MemberEntity.sortByAgeDesc ());
Console.WriteLine("List -Sorted by Age Desc");
foreach (MemberEntity m in memberList)
{
Console.WriteLine(m.Name + "\t\t" + m.Age.ToString());
}
Console.WriteLine("it is ok,按任意键结束");
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
using System.Text;
using System.Data;
using System.IO;
using System.Xml.XPath;
using System.Xml;
namespace ConsoleApplication1
{
class Program
{
ArrayList memberList = new ArrayList ()
{
new MemberEntity(){ Name="aaa", Age =12},
new MemberEntity(){ Name="ccc" ,Age=14},
new MemberEntity(){ Name="bbb", Age=15}
};
Console.WriteLine("List -UnSorted\n");
foreach (MemberEntity m in memberList)
{
Console.WriteLine(m.Name +"\t\t"+m.Age.ToString());
}
memberList.Sort();
Console.WriteLine("List -Sorted by Name Asc");
foreach (MemberEntity m in memberList)
{
Console.WriteLine(m.Name + "\t\t" + m.Age.ToString());
}
memberList.Sort(MemberEntity.sortByNameDesc());
Console.WriteLine("List -Sorted by Name Desc");
foreach (MemberEntity m in memberList)
{
Console.WriteLine(m.Name + "\t\t" + m.Age.ToString());
}
memberList.Sort(MemberEntity.sortByAgeAsc ());
Console.WriteLine("List -Sorted by Age Asc");
foreach (MemberEntity m in memberList)
{
Console.WriteLine(m.Name + "\t\t" + m.Age.ToString());
}
memberList.Sort(MemberEntity.sortByAgeDesc ());
Console.WriteLine("List -Sorted by Age Desc");
foreach (MemberEntity m in memberList)
{
Console.WriteLine(m.Name + "\t\t" + m.Age.ToString());
}
Console.WriteLine("it is ok,按任意键结束");
Console.ReadKey();
}
}
}
比较结果截图