惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The GitHub Blog
The GitHub Blog
S
SegmentFault 最新的问题
MyScale Blog
MyScale Blog
有赞技术团队
有赞技术团队
V
Visual Studio Blog
T
The Blog of Author Tim Ferriss
爱范儿
爱范儿
Vercel News
Vercel News
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Y
Y Combinator Blog
Blog — PlanetScale
Blog — PlanetScale
D
DataBreaches.Net
美团技术团队
Microsoft Security Blog
Microsoft Security Blog
大猫的无限游戏
大猫的无限游戏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
酷 壳 – CoolShell
酷 壳 – CoolShell
GbyAI
GbyAI
A
About on SuperTechFans
云风的 BLOG
云风的 BLOG
The Cloudflare Blog
宝玉的分享
宝玉的分享
V
V2EX
Microsoft Azure Blog
Microsoft Azure Blog

博客园 - james.dong

初次使用T4引擎生成数据库表实体 - james.dong - 博客园 加快大表关联查询的速度(oracle) DataTable 排序 - james.dong - 博客园 查询oracle数据库中所有视图和表的信息 日期验证正规表达式( YYYY-MM-DD YYYY-MM-DD hh:mm:ss YYYY/MM/DD) windows2000server下 iis 无法下载 .exe , .dll文件的解决办法。 - james.dong C#中datagridview使用技巧系列谈(-)让输入焦点从左到右收藏 oracle中创建自增字段 .net 序列化和反序列化自定义treenode类 LotusScript基础知识(二) LotusScript基本语法知识(一) LotusScript中Option 的含义 Lotusscript中Instr()函数的功能和用法 System.Xml.XmlDocument.SelectNodes() 查询不到节点问题? - james.dong - 博客园 Combox控件实现类似TextBox控件的ReadOnly=true时的背景颜色和字体颜色!(WinForm) WPF 中的 BitmapEffect 的 各种样式 WPF相关文章汇总 asp.net2.0解决用户控件图片相对路径出错的问题,ResolveUrl的用法 window.showModalDialog()时没有显示修改后的数据 - james.dong - 博客园
使用List泛型,怎么排序
james.dong · 2008-07-04 · via 博客园 - james.dong
   

  using   System;  
  
using   System.Collections.Generic;  
  
using   System.Text;  
   
  
namespace   Ch12Ex02  
  
{  
        
class   Program  
        
{  
              
static   void   Main(string[]   args)  
              
                       
                    
//Collection<Animal>   animalCollection   =   new   Collection<Animal>();  
                    List<Animal>   animalCollection   =   new   List<Animal>();  
                    animalCollection.Add(
new   Cow("Jack"));  
                    animalCollection.Add(
new   Chicken("Vera"));  
                    
//animalCollection.Sort();//此处编译错误,请问是怎么回事?  
                    foreach   (Animal   myAnimal   in   animalCollection)  
                    
{  
                          myAnimal.Feed();  
                    }
  
                    Console.ReadKey();  
              }
  
        }
  
  }
  
 

   原来

用的animalCollection.Sort()这个是用的默认的比较器...  

  对于list<T>来说:  
  此方法使用类型   T   的默认比较器   Comparer.Default   确定列表元素的顺序。Comparer.Default   属性检查类型   T   是否实现了   IComparable   泛型接口,如果实现了该接口,则使用该实现。否则,Comparer.Default   将检查类型   T   是否实现了   IComparable   接口。如果类型   T   未实现任一接口,则   Comparer.Default   将引发   InvalidOperationException。  
   
  也就是说Animal没有实现IComparable   接口,它也不知道怎么帮你排啊?按照什么排啊?

对于范型来说必须指定比较器  
  可以这样做:  
  1.   让Animal实现IComparable接口,实现CompareTo方法(假设以name来排序):  
  class   Animal   :   IComparable<Animal>  
  {  
      ...  
      public   int   CompareTo(Animal   other)  
      {  
            return   name.CompareTo(other.name);  
      }  
  }  
  这样直接调用sort就可以排序   list.Sort();  
  2.自己写ICompare比较器:  
  class   AnimalCompare   :   IComparer<Animal>  
  {  
          public   int   Compare(Animal   a,   Animal   b)  
          {  
                  return   a.name.CompareTo(b.name);  
          }  
  }  
  这样使用   list.Sort(new   AnimalCompare());

也可以使用   SortedList<key,   value>    
  在Add元素的时候指定一个key,这样SortedList会自动以key的Compare方法进行排序  
  比如    
  SortedList<string,   Animal>   sl   =   new   SortedList<string,   Animal>();  
  sl.Add("animal1",   new   Animal());  
  sl.Add("animal2",   new Anmial());