c#泛型集合排序总结
今天学习学累了,还有一点时间就总结一下重要的知识点吧!也算复习了!
排序有很多方法其中有很多都是被接受的(即老师上课讲的),当时虽然也会用但是很不理解为什么要这样做,这样做为什么能实现,其实细研究一下也没什么,只不过微软给我们封装了很多东西,所以导致现在学习编程的人,尤其是学习所谓的高级语言的人,都只知道什么东西怎么用,能干什么,更多的是去关心业务逻辑,而不关心底层实现方式,所以现在当有很多东西可以实现同一个功能时,有很多人知道在什么情况下用什么方式最好,但是为什么最好,不知道!只知道好!所以都比较迷惑。(下面几种方法我也不知道那种好,但都能实现排序功能)
方法一:传统的实现接口的方法(参看MSDN)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ConsoleApplication1
{
/// <summary>
/// 泛型集合排序
/// </summary>
class Program
{
static void Main(string[] args)
{
List<Student> list = new List<Student>();

list.Add(new Student ("A001","张三",18));
list.Add(new Student("A005", "张四",17));
list.Add(new Student("A003", "张五", 15));
MySort my = new MySort();
list.Sort(my);

foreach (Student s in list )
{
Console.WriteLine(s.Id+s.Name+s.Age );
}
}
}

public class MySort : IComparer<Student>
{

public int Compare(Student x, Student y)
{
//return x.Id.CompareTo(y.Id );
return string.Compare(x.Id, y.Id);//这两句都可以
}
}

//学生类
public class Student
{
private string id;

public string Id
{
get { return id; }
set { id = value; }
}
private string name;

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

public int Age
{
get { return age; }
set { age = value; }
}
public Student(string id, string name, int age)
{
this.id = id;
this.name = name;
this.age = age;
}
}
}
方法二:利用反射(比较通用,半夜突然想起来的,一测试就成功了!)
任何自定义的类组成的泛型集合都可以实现自定义排序,可以做成一个通用类,没有必要每一个自定义类都得实现排序的接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;


namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Student s = new Student();
s.Name = "李三";

List<Student> list = new List<Student>();
list.Add(s);
s = new Student();
s.Name = "李四";
list.Add(s);
s = new Student();
s.Name = "李五";
list.Add(s);
Mysort<Student> sort = new Mysort<Student>();
sort.SortOrder = "ASC";
sort.SortString = "Name";
list.Sort(sort);
foreach ( Student st in list )
{
Console.WriteLine(st.Name.ToString());
}
}
}
public class Student
{
private string id;

public string Id
{
get { return id; }
set { id = value; }
}

private string name;

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

public int Age
{
get { return age; }
set { age = value; }
}
}
public class Mysort<T> : IComparer<T>
{

private string sortOrder;

public string SortOrder
{
get { return sortOrder; }
set { sortOrder = value; }
}

private string sortString;

public string SortString
{
get { return sortString; }
set { sortString = value; }
}

public int Compare(T x, T y)
{
if(sortOrder=="ASC")
{
return (y.GetType().GetProperty(sortString).GetValue(y,null).ToString()).CompareTo(x.GetType()
.GetProperty(sortString).GetValue(x,null).ToString());
}
else
{
return (x.GetType().GetProperty(sortString).GetValue(x,null).ToString()).CompareTo(y.GetType()
.GetProperty(sortString).GetValue(y,null).ToString());
}
}


}
}

三:使用linq中的一些方法也可以实现,就更简单了,不过要想实现通用的,还得考虑在其中用一下反射内容
使用linq的方法实现的就不再举例了(可以参考VS2010的帮助文档,VS2010的帮助文档挺强大!)
下面是一个使用linq和反射技术结合的例子(因为这样可以做到通用)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 排序
{
class Program
{

static void Main(string[] args)
{
Student s = new Student();
s.Name = "李三";
List<Student> list = new List<Student>();
list.Add(s);
s = new Student();
s.Name = "李四";
list.Add(s);
s = new Student();
s.Name = "李五";
list.Add(s);
list.Sort((Student x,Student y)=>((y.GetType().GetProperty

("Name").GetValue(y, null).ToString()).CompareTo(x.GetType().GetProperty

("Name").GetValue(x, null).ToString())));
foreach ( Student st in list )
{
Console.WriteLine(st.Name.ToString());
}
}
}
}
public class Student
{
private string id;

public string Id
{
get { return id; }
set { id = value; }
}

private string name;

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

public int Age
{
get { return age; }
set { age = value; }
}
}
总结:现在什么东西都变得越来越简单,可能唯一变得复杂的就是需求!还有一点就是什么东西想好了再去做,效果会好一点!