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

推荐订阅源

阮一峰的网络日志
阮一峰的网络日志
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
Schneier on Security
The Last Watchdog
The Last Watchdog
Cyberwarzone
Cyberwarzone
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cyber Attacks, Cyber Crime and Cyber Security
L
Lohrmann on Cybersecurity
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 司徒正美
The Cloudflare Blog
V
V2EX
博客园_首页
博客园 - 聂微东
Vercel News
Vercel News
人人都是产品经理
人人都是产品经理
G
GRAHAM CLULEY
T
Tenable Blog
Last Week in AI
Last Week in AI
Y
Y Combinator Blog
L
LINUX DO - 最新话题
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
SecWiki News
SecWiki News
博客园 - 三生石上(FineUI控件)
S
Secure Thoughts
N
News | PayPal Newsroom
T
The Blog of Author Tim Ferriss
The GitHub Blog
The GitHub Blog
T
Troy Hunt's Blog
博客园 - 【当耐特】
Forbes - Security
Forbes - Security
H
Hacker News: Front Page
A
About on SuperTechFans
B
Blog RSS Feed
Engineering at Meta
Engineering at Meta
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
罗磊的独立博客
D
DataBreaches.Net
P
Privacy & Cybersecurity Law Blog
Schneier on Security
Schneier on Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Google DeepMind News
Google DeepMind News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Jina AI
Jina AI
D
Docker
P
Proofpoint News Feed

博客园 - 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());