自定义对象的比较系列二之实现IComparable<T>
2009-05-07 12:09 Virus-BeautyCode 阅读(398) 评论(0) 编辑 收藏 举报
要进行排序的对象
MemberEntity11
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class MemberEntity11 : IComparable<MemberEntity11>
{
#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(MemberEntity11 obj)
{
MemberEntity11 member = obj as MemberEntity11;
return string.Compare(this._name, member._name);
}
private class sortByAgeAscHelper : IComparer<MemberEntity11>
{
public int Compare(MemberEntity11 a, MemberEntity11 b)
{
MemberEntity11 m1 = a as MemberEntity11;
MemberEntity11 m2 = b as MemberEntity11;
if (m1._age > m2._age)
return 1;
if (m1._age < m2._age)
return -1;
else
return 0;
}
}
private class sortByAgeDescHelper : IComparer<MemberEntity11>
{
public int Compare(MemberEntity11 a, MemberEntity11 b)
{
MemberEntity11 m1 = a as MemberEntity11;
MemberEntity11 m2 = b as MemberEntity11;
if (m1._age < m2._age)
return 1;
if (m1._age > m2._age)
return -1;
else
return 0;
}
}
private class sortByNameDescHelper : IComparer<MemberEntity11>
{
public int Compare(MemberEntity11 a, MemberEntity11 b)
{
MemberEntity11 m1 = a as MemberEntity11;
MemberEntity11 m2 = b as MemberEntity11;
return string.Compare(m2._name, m1._name);
}
}
public static IComparer<MemberEntity11> sortByAgeAsc()
{
return (IComparer<MemberEntity11>)new sortByAgeAscHelper();
}
public static IComparer<MemberEntity11> sortByAgeDesc()
{
return (IComparer<MemberEntity11>)new sortByAgeDescHelper();
}
public static IComparer<MemberEntity11> sortByNameDesc()
{
return (IComparer<MemberEntity11>)new sortByNameDescHelper();
}
}
}
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class MemberEntity11 : IComparable<MemberEntity11>
{
#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(MemberEntity11 obj)
{
MemberEntity11 member = obj as MemberEntity11;
return string.Compare(this._name, member._name);
}
private class sortByAgeAscHelper : IComparer<MemberEntity11>
{
public int Compare(MemberEntity11 a, MemberEntity11 b)
{
MemberEntity11 m1 = a as MemberEntity11;
MemberEntity11 m2 = b as MemberEntity11;
if (m1._age > m2._age)
return 1;
if (m1._age < m2._age)
return -1;
else
return 0;
}
}
private class sortByAgeDescHelper : IComparer<MemberEntity11>
{
public int Compare(MemberEntity11 a, MemberEntity11 b)
{
MemberEntity11 m1 = a as MemberEntity11;
MemberEntity11 m2 = b as MemberEntity11;
if (m1._age < m2._age)
return 1;
if (m1._age > m2._age)
return -1;
else
return 0;
}
}
private class sortByNameDescHelper : IComparer<MemberEntity11>
{
public int Compare(MemberEntity11 a, MemberEntity11 b)
{
MemberEntity11 m1 = a as MemberEntity11;
MemberEntity11 m2 = b as MemberEntity11;
return string.Compare(m2._name, m1._name);
}
}
public static IComparer<MemberEntity11> sortByAgeAsc()
{
return (IComparer<MemberEntity11>)new sortByAgeAscHelper();
}
public static IComparer<MemberEntity11> sortByAgeDesc()
{
return (IComparer<MemberEntity11>)new sortByAgeDescHelper();
}
public static IComparer<MemberEntity11> sortByNameDesc()
{
return (IComparer<MemberEntity11>)new sortByNameDescHelper();
}
}
}
排序示例代码
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
using System.Text;
using Microsoft.SharePoint;
using System.Data;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Publishing;
using System.IO;
using System.Xml.XPath;
using System.Xml;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<MemberEntity11 > memberList = new List<MemberEntity11> ()
{
new MemberEntity11(){ Name="aaa", Age =12},
new MemberEntity11(){ Name="ccc" ,Age=14},
new MemberEntity11(){ Name="bbb", Age=15}
};
Console.WriteLine("List -UnSorted\n");
foreach (MemberEntity11 m in memberList)
{
Console.WriteLine(m.Name +"\t\t"+m.Age.ToString());
}
memberList.Sort();
Console.WriteLine("List -Sorted by Name Asc");
foreach (MemberEntity11 m in memberList)
{
Console.WriteLine(m.Name + "\t\t" + m.Age.ToString());
}
memberList.Sort(MemberEntity11.sortByNameDesc());
Console.WriteLine("List -Sorted by Name Desc");
foreach (MemberEntity11 m in memberList)
{
Console.WriteLine(m.Name + "\t\t" + m.Age.ToString());
}
memberList.Sort(MemberEntity11.sortByAgeAsc ());
Console.WriteLine("List -Sorted by Age Asc");
foreach (MemberEntity11 m in memberList)
{
Console.WriteLine(m.Name + "\t\t" + m.Age.ToString());
}
memberList.Sort(MemberEntity11.sortByAgeDesc ());
Console.WriteLine("List -Sorted by Age Desc");
foreach (MemberEntity11 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 Microsoft.SharePoint;
using System.Data;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Publishing;
using System.IO;
using System.Xml.XPath;
using System.Xml;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<MemberEntity11 > memberList = new List<MemberEntity11> ()
{
new MemberEntity11(){ Name="aaa", Age =12},
new MemberEntity11(){ Name="ccc" ,Age=14},
new MemberEntity11(){ Name="bbb", Age=15}
};
Console.WriteLine("List -UnSorted\n");
foreach (MemberEntity11 m in memberList)
{
Console.WriteLine(m.Name +"\t\t"+m.Age.ToString());
}
memberList.Sort();
Console.WriteLine("List -Sorted by Name Asc");
foreach (MemberEntity11 m in memberList)
{
Console.WriteLine(m.Name + "\t\t" + m.Age.ToString());
}
memberList.Sort(MemberEntity11.sortByNameDesc());
Console.WriteLine("List -Sorted by Name Desc");
foreach (MemberEntity11 m in memberList)
{
Console.WriteLine(m.Name + "\t\t" + m.Age.ToString());
}
memberList.Sort(MemberEntity11.sortByAgeAsc ());
Console.WriteLine("List -Sorted by Age Asc");
foreach (MemberEntity11 m in memberList)
{
Console.WriteLine(m.Name + "\t\t" + m.Age.ToString());
}
memberList.Sort(MemberEntity11.sortByAgeDesc ());
Console.WriteLine("List -Sorted by Age Desc");
foreach (MemberEntity11 m in memberList)
{
Console.WriteLine(m.Name + "\t\t" + m.Age.ToString());
}
Console.WriteLine("it is ok,按任意键结束");
Console.ReadKey();
}
}
}
排序结果图