欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#中Dictionary<TKey,TValue>排序方式的實現(xiàn)

 更新時間:2021年02月26日 10:32:45   作者:浮海揚(yáng)塵  
這篇文章主要介紹了C#中Dictionary<TKey,TValue>排序方式的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

自定義類:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharp中Dictionary排序方式
{
  [Serializable]
  public class CustmonizedClass
  {
    public string stuName { get; set; }

    public int stuAge { get; set; }

    public string stuSex { get; set; }

    public double stuScore { get; set; }
    
  }
}

Dictionary<int,自定義類>

按照Dictionary的Key值 升序排序(OrderBy)、降序排序(OrderByDescending):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharp中Dictionary排序方式
{
   public class Program
  {
    static void Main(string[] args)
    {
      CustmonizedClass cn1 = new CustmonizedClass();
      cn1.stuName = "張三";
      cn1.stuAge = 18;
      cn1.stuSex = "男";
      cn1.stuScore = 89.5;

      CustmonizedClass cn2 = new CustmonizedClass();
      cn2.stuName = "李四";
      cn2.stuAge = 19;
      cn2.stuSex = "男";
      cn2.stuScore = 88.5;


      CustmonizedClass cn3 = new CustmonizedClass();
      cn3.stuName = "王五";
      cn3.stuAge = 17;
      cn3.stuSex = "女";
      cn3.stuScore = 89.5;

      Dictionary<int, CustmonizedClass> dic1 = new Dictionary<int, CustmonizedClass>();
      dic1.Add(3, cn1);
      dic1.Add(1, cn2);
      dic1.Add(2, cn3);
      //上面dic1.Add()故意不按照順序

      Dictionary<int, CustmonizedClass> dic1_SortedByKey = dic1.OrderBy(p=>p.Key).ToDictionary(p => p.Key, o => o.Value);
      

      foreach (KeyValuePair<int, CustmonizedClass> item in dic1_SortedByKey) 
      {
        Console.WriteLine("Key:{0} ; Value: name:{1}, age:{2}, sex:{3}, score:{4} ",
          item.Key,item.Value.stuName,item.Value.stuAge,item.Value.stuSex,item.Value.stuScore);
      }
      Console.ReadLine();            
    }
  }
}
Dictionary<int, CustmonizedClass> dic1_SortedByKey = dic1.OrderBy(p=>p.Key).ToDictionary(p => p.Key, o => o.Value);

結(jié)果截圖:

降序排序:

Dictionary<int, CustmonizedClass> dic1_SortedByKey = dic1.OrderByDescending(p => p.Key).ToDictionary(p => p.Key, o => o.Value);

結(jié)果截圖:

按照Dictionary的Value值的某個屬性 升序排序(OrderBy)、降序排序(OrderByDescending):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharp中Dictionary排序方式
{
   public class Program
  {
    static void Main(string[] args)
    {
      CustmonizedClass cn1 = new CustmonizedClass();
      cn1.stuName = "張三";
      cn1.stuAge = 18;
      cn1.stuSex = "男";
      cn1.stuScore = 89.5;

      CustmonizedClass cn2 = new CustmonizedClass();
      cn2.stuName = "李四";
      cn2.stuAge = 19;
      cn2.stuSex = "男";
      cn2.stuScore = 88.5;


      CustmonizedClass cn3 = new CustmonizedClass();
      cn3.stuName = "王五";
      cn3.stuAge = 17;
      cn3.stuSex = "女";
      cn3.stuScore = 89.5;

      Dictionary<int, CustmonizedClass> dic1 = new Dictionary<int, CustmonizedClass>();
      dic1.Add(3, cn1);
      dic1.Add(1, cn2);
      dic1.Add(2, cn3);
      //上面dic1.Add()故意不按照順序
      //Key升序
      //Dictionary<int, CustmonizedClass> dic1_SortedByKey = dic1.OrderBy(p=>p.Key).ToDictionary(p => p.Key, o => o.Value);
      //Key降序
      //Dictionary<int, CustmonizedClass> dic1_SortedByKey = dic1.OrderByDescending(p => p.Key).ToDictionary(p => p.Key, o => o.Value);
      //Value中stuAge屬性
      Dictionary<int, CustmonizedClass> dic1_SortedByKey = dic1.OrderBy(o => o.Value.stuAge).ToDictionary(p => p.Key, o => o.Value);

      foreach (KeyValuePair<int, CustmonizedClass> item in dic1_SortedByKey) 
      {
        Console.WriteLine("Key:{0} ; Value: name:{1}, age:{2}, sex:{3}, score:{4} ",
          item.Key,item.Value.stuName,item.Value.stuAge,item.Value.stuSex,item.Value.stuScore);
      }
      Console.ReadLine();            
    }
  }
}

關(guān)鍵修改這句:

Dictionary<int, CustmonizedClass> dic1_SortedByKey = dic1.OrderBy(o => o.Value.stuAge).ToDictionary(p=>p.Key,o=>o.Value);

結(jié)果截圖:

混合排序:類似EXCEL中先按第一列升序、再按第3列的升序……

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharp中Dictionary排序方式
{
   public class Program
  {
    static void Main(string[] args)
    {
      CustmonizedClass cn1 = new CustmonizedClass();
      cn1.stuName = "張三";
      cn1.stuAge = 18;
      cn1.stuSex = "男";
      cn1.stuScore = 89.5;

      CustmonizedClass cn2 = new CustmonizedClass();
      cn2.stuName = "李四";
      cn2.stuAge = 19;
      cn2.stuSex = "男";
      cn2.stuScore = 88.5;


      CustmonizedClass cn3 = new CustmonizedClass();
      cn3.stuName = "王五";
      cn3.stuAge = 17;
      cn3.stuSex = "女";
      cn3.stuScore = 89.5;

      Dictionary<int, CustmonizedClass> dic1 = new Dictionary<int, CustmonizedClass>();
      dic1.Add(3, cn1);
      dic1.Add(1, cn2);
      dic1.Add(2, cn3);
      //上面dic1.Add()故意不按照順序
      //Key升序
      //Dictionary<int, CustmonizedClass> dic1_SortedByKey = dic1.OrderBy(p=>p.Key).ToDictionary(p => p.Key, o => o.Value);
      //Key降序
      //Dictionary<int, CustmonizedClass> dic1_SortedByKey = dic1.OrderByDescending(p => p.Key).ToDictionary(p => p.Key, o => o.Value);
      //Value中stuAge屬性
      //Dictionary<int, CustmonizedClass> dic1_SortedByKey = dic1.OrderBy(o => o.Value.stuAge).ToDictionary(p => p.Key, o => o.Value);
      //混合排序 等同于下列的linq語句
      //Dictionary<int, CustmonizedClass> dic1_SortedByKey = dic1.OrderBy(o => o.Value.stuScore).ThenByDescending(o=>o.Value.stuAge).ToDictionary(p=>p.Key,o=>o.Value);

      //linq語句
      var dic1_SortedByKey = from n in dic1

             orderby n.Value.stuScore, n.Value.stuAge descending

             select n;

      foreach (KeyValuePair<int, CustmonizedClass> item in dic1_SortedByKey) 
      {
        Console.WriteLine("Key:{0} ; Value: name:{1}, age:{2}, sex:{3}, score:{4} ",
          item.Key,item.Value.stuName,item.Value.stuAge,item.Value.stuSex,item.Value.stuScore);
      }
      Console.ReadLine();            
    }
  }
}
Dictionary<int, CustmonizedClass> dic1_SortedByKey = dic1.OrderBy(o => o.Value.stuScore).ThenByDescending(o=>o.Value.stuAge).ToDictionary(p=>p.Key,o=>o.Value);

等同于linq語句:

var dic1_SortedByKey = from n in dic1

orderby n.Value.stuScore, n.Value.stuAge descending

select n;

結(jié)果截圖:


到此這篇關(guān)于C#中Dictionary<TKey,TValue>排序方式的實現(xiàn)的文章就介紹到這了,更多相關(guān)C# Dictionary<TKey,TValue>排序內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 在多線程中調(diào)用winform窗體控件的實現(xiàn)方法

    在多線程中調(diào)用winform窗體控件的實現(xiàn)方法

    這篇文章主要介紹了在多線程中調(diào)用winform窗體控件的實現(xiàn)方法,需要的朋友可以參考下
    2014-08-08
  • C#中Invoke和BeginInvoke區(qū)別小結(jié)

    C#中Invoke和BeginInvoke區(qū)別小結(jié)

    有時候,我們不得不跨線程調(diào)用主界面的控件來進(jìn)行操作,所以為了方便的解決問題,.net為我們提供了Invoke?與beginInvoke,那么Invoke和BeginInvoke區(qū)別在哪,本文就來詳細(xì)的介紹一下
    2023-08-08
  • Unity幸運(yùn)轉(zhuǎn)盤實戰(zhàn)項目

    Unity幸運(yùn)轉(zhuǎn)盤實戰(zhàn)項目

    這篇文章主要為大家詳細(xì)介紹了Unity幸運(yùn)轉(zhuǎn)盤實戰(zhàn)項目,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • DirectoryInfo引用一個相對目錄的實例

    DirectoryInfo引用一個相對目錄的實例

    這種特殊參數(shù)在Windows的命令提示符或者“運(yùn)行”對話框中都可以使用,等價于DOS中的cd命令參數(shù)。直接上代碼,一看你就懂了:
    2013-04-04
  • C#中泛型容器Stack<T>的用法并實現(xiàn)”撤銷/重做”功能

    C#中泛型容器Stack<T>的用法并實現(xiàn)”撤銷/重做”功能

    這篇文章介紹了C#中泛型容器Stack<T>的用法并實現(xiàn)”撤銷/重做”功能,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-10-10
  • 在C#中捕獲內(nèi)存不足異常

    在C#中捕獲內(nèi)存不足異常

    這篇文章主要介紹了在C#中捕獲內(nèi)存不足異常,下面文章內(nèi)容圍繞如何在C#中捕獲內(nèi)存不足異常的相關(guān)資料展開詳細(xì)內(nèi)容,具有一定的參考價值,需要的小伙伴可以參考一下,希望對你有所幫助
    2021-12-12
  • C#無損轉(zhuǎn)換Image為Icon的方法

    C#無損轉(zhuǎn)換Image為Icon的方法

    這篇文章主要為大家詳細(xì)介紹了C#無損轉(zhuǎn)換Image為Icon的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • C#繼承IList?接口的實現(xiàn)步驟

    C#繼承IList?接口的實現(xiàn)步驟

    C#中的IList<T>接口是.NET框架中的一種通用接口,它定義了一組在運(yùn)行時可以使用類型參數(shù)T的元素的集合,本文給大家介紹了C#繼承IList?接口的設(shè)計方法,文中通過代碼示例給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2024-02-02
  • C#字符串String及字符Char的相關(guān)方法

    C#字符串String及字符Char的相關(guān)方法

    這篇文章介紹了C#字符串String及字符Char的相關(guān)方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-05-05
  • unity自定義彈出框功能

    unity自定義彈出框功能

    這篇文章主要為大家詳細(xì)介紹了unity自定義彈出框功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-11-11

最新評論