C#中Dictionary<TKey,TValue>排序方式的实现

 更新时间:2021年02月26日 10:32:45   作者:浮海扬尘  
这篇文章主要介绍了C#中Dictionary<TKey,TValue>排序方式的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

自定义类:

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);

结果截图:

降序排序:

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

结果截图:

按照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();            
    }
  }
}

关键修改这句:

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

结果截图:

混合排序:类似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;

结果截图:


到此这篇关于C#中Dictionary<TKey,TValue>排序方式的实现的文章就介绍到这了,更多相关C# Dictionary<TKey,TValue>排序内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • C#读写文件的方法汇总

    C#读写文件的方法汇总

    C#读写文件的方法汇总,需要的朋友可以参考一下
    2013-03-03
  • C#使用dynamic一行代码实现反射操作

    C#使用dynamic一行代码实现反射操作

    dynamic的出现让C#具有了弱语言类型的特性。编译器在编译的时候不再对类型进行检查,编译时默认dynamic对象支持你想要的任何特性,这篇文章主要介绍了C#用dynamic一行代码实现反射操作,需要的朋友可以参考下
    2023-04-04
  • 浅谈C#中的Async和Await的用法详解

    浅谈C#中的Async和Await的用法详解

    这篇文章主要介绍了浅谈C#中的Async和Await的用法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-03-03
  • C#调用百度翻译API实现一个翻译功能

    C#调用百度翻译API实现一个翻译功能

    一直喜欢用Google Translate API进行在线翻译,但是服务越来越慢这篇文章,所以只能换一个了,主要给大家介绍了关于C#调用百度翻译API实现一个翻译功能的相关资料,需要的朋友可以参考下
    2021-06-06
  • C#使用IronPython调用Python的实现

    C#使用IronPython调用Python的实现

    本文主要介绍了C#使用IronPython调用Python的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-02-02
  • 基于C#后台调用跨域MVC服务及带Cookie验证的实现

    基于C#后台调用跨域MVC服务及带Cookie验证的实现

    本篇文章介绍了,基于C#后台调用跨域MVC服务及带Cookie验证的实现。需要的朋友参考下
    2013-04-04
  • C# 列表List的常用属性和方法介绍

    C# 列表List的常用属性和方法介绍

    这篇文章主要介绍了C# 列表List的常用属性和方法介绍,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-04-04
  • Unity3D移动端实现摇一摇功能

    Unity3D移动端实现摇一摇功能

    这篇文章主要为大家详细介绍了基于Unity3D移动端实现摇一摇功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-10-10
  • C# Partial:分部方法和分部类代码实例

    C# Partial:分部方法和分部类代码实例

    这篇文章主要介绍了C# Partial:分部方法和分部类代码实例,本文直接给出代码实现,需要的朋友可以参考下
    2015-03-03
  • C#生成PDF文件流

    C#生成PDF文件流

    这篇文章主要为大家详细介绍了C#生成PDF文件流的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03

最新评论