using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GenericCompare
{
class Program
{
static void Main( string [] args)
{
List < Student > students = new List < Student > ();
students.Add( new Student( " 001 " , " kenshincui " , 25 ));
students.Add( new Student( " 002 " , " miaoer " , 23 ));
students.Add( new Student( " 003 " , " shenjinjuan " , 22 ));
students.Add( new Student( " 004 " , " nieyanxin " , 24 ));
Console.WriteLine( " 未进行排序之前: " );
foreach (Student st in students)
{
Console.WriteLine(st.No + " , " + st.Name + " , " + st.Age + " ; " );
}
Console.WriteLine( " List.Sort (Int32, Int32, 泛型 IComparer) 排序之后: " );
students.Sort( 0 , 3 , new StudentCompare());
foreach (Student st in students)
{
Console.WriteLine(st.No + " , " + st.Name + " , " + st.Age + " ; " );
}
Console.ReadKey();
}
}
}
运行效果:
五、总结:
对于List的排序,.Net中给我们提供了很多选择,我们可以根据情况灵活使用。关于其他泛型集合的排序(例如Dictionary)有时间的话我后面也会逐一同大家分享,今天暂且到这里吧!
本作品采用知识共享署名 2.5 中国大陆许可协议进行许可,欢迎转载,演绎或用于商业目的。但转载请注明来自崔江涛(KenshinCui),并包含相关链接。 |