当前位置:   article > 正文

C# 字典 Dictionary 排序_c# dictionary 排序

c# dictionary 排序

前言

C# Dictionary<TKey,TValue> 表示键和值的集合,可以很方便的使用linq进行排序操作。

开始

简单类型排序

以<string,string>类型为例: 新建控制台应用,并用下面代码替换掉 Program.cs 的代码

using System;
using System.Collections.Generic;
using System.Linq;

namespace DicSort
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("kvs: \n---------");
            var kvs = new Dictionary<string, string>();
            kvs.Add("name", "wufan");
            kvs.Add("id", "1");
            kvs.Add("age", "26");
            PrintDic(kvs);
            Console.WriteLine("--------\n\n orderedKvs: \n---------");
            var orderedKvs= kvs.OrderBy(x => x.Key).ToDictionary(x=>x.Key,x=>x.Value);
            PrintDic(orderedKvs);

            Console.Read();
        }

        st
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/84488
推荐阅读
  

闽ICP备14008679号