最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2021年02月26日 10:32:45   作者:浮海揚塵  
這篇文章主要介紹了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)文章

  • C#使用DLLImport調(diào)用外部DLL的方法

    C#使用DLLImport調(diào)用外部DLL的方法

    這篇文章介紹了C#使用DLLImport調(diào)用外部DLL的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • C#控制臺帶參數(shù)程序源碼編寫實例講解

    C#控制臺帶參數(shù)程序源碼編寫實例講解

    像ipconfig /all 這樣的CMD命令想必大家都知道,但是很多童鞋可能不知道怎么寫這樣的控制臺帶參數(shù)的程序,需要的朋友可以了解下
    2012-12-12
  • C#中靜態(tài)構(gòu)造函數(shù)的幾點說明介紹

    C#中靜態(tài)構(gòu)造函數(shù)的幾點說明介紹

    本篇文章主要是對C#中靜態(tài)構(gòu)造函數(shù)的幾點說明進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助
    2014-01-01
  • C# Newtonsoft.Json用法詳解

    C# Newtonsoft.Json用法詳解

    本文主要介紹了C# Newtonsoft.Json用法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • C#調(diào)用C++的實現(xiàn)步驟

    C#調(diào)用C++的實現(xiàn)步驟

    本文主要介紹了C#調(diào)用C++的基本規(guī)則和方法,包括內(nèi)存對齊、調(diào)用約定、基本數(shù)據(jù)類型的傳遞、結(jié)構(gòu)體的傳遞以及數(shù)組的傳遞等,感興趣的可以了解一下
    2024-11-11
  • C# Color.FromArgb()及系統(tǒng)顏色對照表一覽

    C# Color.FromArgb()及系統(tǒng)顏色對照表一覽

    這篇文章主要介紹了C# Color.FromArgb()及系統(tǒng)顏色對照表一覽,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • C#?wpf?通過HwndHost渲染視頻的實現(xiàn)方法

    C#?wpf?通過HwndHost渲染視頻的實現(xiàn)方法

    日常開發(fā)中,特別是音視頻開發(fā),需要在界面上渲染視頻,比如制作一個播放器、或者視頻編輯工具、以及視頻會議客戶端。通常拿到的是像素格式數(shù)據(jù),此時需要渲染到wpf窗口上就需要一定的方法,本文介紹一種通過hwnd渲染的方法,控件既能提供hwnd又能嵌入wpf窗口里
    2021-11-11
  • 利用WCF雙工模式實現(xiàn)即時通訊

    利用WCF雙工模式實現(xiàn)即時通訊

    這篇文章主要介紹了利用WCF雙工模式實現(xiàn)即時通訊的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • C#中IEnumerable接口介紹并實現(xiàn)自定義集合

    C#中IEnumerable接口介紹并實現(xiàn)自定義集合

    這篇文章介紹了C#中IEnumerable接口并實現(xiàn)自定義集合,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • C#?使用PrintDocument類打印標(biāo)簽的方法

    C#?使用PrintDocument類打印標(biāo)簽的方法

    本文介紹打印機(jī)初步配置,以及實現(xiàn)方法,標(biāo)簽主要展示資產(chǎn)基本信息以及二維碼,對C#?使用PrintDocument類打印標(biāo)簽的詳細(xì)過程感興趣的朋友一起看看吧
    2022-04-04

最新評論

阿拉尔市| 香格里拉县| 漾濞| 留坝县| 沅陵县| 临泉县| 蓬莱市| 福建省| 株洲县| 临泽县| 阿克| 政和县| 尚义县| 潢川县| 西城区| 河曲县| 湖北省| 通州区| 遂溪县| 永济市| 南城县| 图们市| 海口市| 曲水县| 东兰县| 桂东县| 慈利县| 内乡县| 肇庆市| 寿光市| 定兴县| 德清县| 涞水县| 临武县| 阿拉善右旗| 尚志市| 会理县| 上饶市| 延安市| 巨野县| 虹口区|