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

推荐订阅源

Google DeepMind News
Google DeepMind News
Stack Overflow Blog
Stack Overflow Blog
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
T
The Blog of Author Tim Ferriss
博客园 - 叶小钗
N
Netflix TechBlog - Medium
腾讯CDC
C
Check Point Blog
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
S
SegmentFault 最新的问题
F
Fortinet All Blogs
美团技术团队
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 司徒正美
F
Full Disclosure
Recorded Future
Recorded Future
D
DataBreaches.Net
博客园 - 【当耐特】
Martin Fowler
Martin Fowler
J
Java Code Geeks
I
InfoQ
Y
Y Combinator Blog
A
About on SuperTechFans
AI
AI
爱范儿
爱范儿
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Forbes - Security
Forbes - Security
W
WeLiveSecurity
M
MIT News - Artificial intelligence
雷峰网
雷峰网
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
Schneier on Security
Schneier on Security
The GitHub Blog
The GitHub Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
Latest news
Latest news
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
D
Docker
Recent Commits to openclaw:main
Recent Commits to openclaw:main
量子位
V2EX - 技术
V2EX - 技术
Project Zero
Project Zero

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