乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      C# Sort排序

       kiki的號 2017-05-08

      List 的Sort方法排序有三種結(jié)果 1,0,-1分別表示大于,等于,小于。

      1.對于數(shù)值類型的List (List<int>),直接使用Sort進(jìn)行排序。

      [csharp] view plain copy
      1. List<int> scoreList=new List<int>(){89,100,78,23,67};  
      2.   
      3. scoreList.Sort();//默認(rèn)按升序排列,相當(dāng)于:scoreList.Sort((x,y)=>x.CompareTo(y))  
      4.   
      5. scoreList.Sort((x,y)=>-x.CompareTo(y));//降序排列  

      2.對于非數(shù)值類型或者自定義類型,可通過實(shí)現(xiàn)IComparable接口重寫CompareTo方法來排序:

      [csharp] view plain copy
      1. public class Person : IComparable<Person>  
      2.     {  
      3.         public string Name { getset; }  
      4.         public int Age { getset; }  
      5.   
      6.         //ComparetTo:大于 1; 等于 0; 小于 -1;  
      7.         public int CompareTo(Person p)  
      8.         {  
      9.             int result;  
      10.             if (this.Name == p.Name && this.Age == p.Age)  
      11.             {  
      12.                 result = 0;  
      13.             }  
      14.             else  
      15.             {  
      16.                 //this.Name表示后面的 Mary p.Name表示前面的 Bob  
      17.                 //Mary 跟Bob 由小到大比較,如果Mary 與 Bob 比較 大于0(說明Mary 大于Bob),則 result=1(說明是由小到大的順序)  
      18.                 if (this.Name.CompareTo(p.Name) > 0)//先按名字小到大排列  
      19.                 {  
      20.                     result = 1;  
      21.                 }  
      22.                 else if (this.Name == p.Name && this.Age > p.Age)//名字相同則按年齡由小到大排列  
      23.                 {  
      24.                     result = 1;  
      25.                 }  
      26.                 else  
      27.                 {  
      28.                     result = -1;  
      29.                 }  
      30.             }  
      31.             return result;  
      32.         }  
      33.   
      34.         public override string ToString()  
      35.         {  
      36.             return this.Name + "-" + this.Age;  
      37.         }  
      38.     }  
      [csharp] view plain copy
      1. List<Person> lstPerson = new List<Person>();  
      2.   lstPerson.Add(new Person() { Name = "Bob", Age = 19 });  
      3.   lstPerson.Add(new Person() { Name = "Mary", Age = 18 });  
      4.   lstPerson.Add(new Person() { Name = "Mary", Age = 17 });  
      5.   lstPerson.Add(new Person() { Name = "Lily", Age = 20 });  
      6.   lstPerson.Sort();  
      7.   foreach (Person item in lstPerson)  
      8.   {  
      9.       Console.WriteLine(item.ToString());  
      10.   }  
      11.   Console.ReadKey();  

      輸出:Bob-19 Lily-20 Mary-17 Mary-18
      或不實(shí)現(xiàn)IComparable接口而使用linq排序:

      [csharp] view plain copy
      1. List<Person> lstPerson = new List<Person>();  
      2.           lstPerson.Add(new Person() { Name = "Bob", Age = 19 });  
      3.           lstPerson.Add(new Person() { Name = "Mary", Age = 18 });  
      4.           lstPerson.Add(new Person() { Name = "Mary", Age = 17 });  
      5.           lstPerson.Add(new Person() { Name = "Lily", Age = 20 });  
      6.           lstPerson.Sort();  
      7.   
      8.           lstPerson.Sort((x, y) => {  
      9.               int result;  
      10.               if (x.Name == y.Name && x.Age == y.Age)  
      11.               {  
      12.                   result = 0;  
      13.               }  
      14.               else  
      15.               {  
      16.                   if (x.Name.CompareTo(y.Name) > 0)  
      17.                   {  
      18.                       result = 1;  
      19.                   }  
      20.                   else if (x.Name == y.Name && x.Age > y.Age)  
      21.                   {  
      22.                       result = 1;  
      23.                   }  
      24.                   else  
      25.                   {  
      26.                       result = -1;  
      27.                   }  
      28.               }  
      29.               return result;  
      30.           });  
      31.   
      32.   
      33.           foreach (Person item in lstPerson)  
      34.           {  
      35.               Console.WriteLine(item.ToString());  
      36.           }  
      37.           Console.ReadKey();  
      輸出:Bob-19 Lily-20 Mary-17 Mary-18



        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多