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>排序内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • C#中查找Dictionary中重复值的方法

    简介 在这篇帮助文档中,我将向你展示如何实现c#里字典中重复值的查找.你知道的对于一个老鸟来说,这是非常简单的代码.但是尽管如此,这也是一篇对c#初学者非常有用的帮助文档. 背景 多数程序员对小型数据源存储的处理方式通常是创建字典进行键值存储.主键时唯一的,但是字典值却可能有重复的元素. 代码 这里我使用了一个简单的LINQ语句来查找字典中的重复值. 复制代码 代码如下: //initialize a dictionary with keys and values.    Dictionary<

  • C#泛型Dictionary的用法实例详解

    本文以实例形式讲述了C#中的泛型Dictionary的用法.具有很好的实用价值.分享给大家供大家参考.具体如下: 泛型最常见的用途是泛型集合,命名空间System.Collections.Generic 中包含了一些基于泛型的集合类,使用泛型集合类可以提供更高的类型安全性,还有更高的性能,避免了非泛型集合的重复的装箱和拆箱. 很多非泛型集合类都有对应的泛型集合类,下面是常用的非泛型集合类以及对应的泛型集合类: 非泛型集合类 泛型集合类 ArrayList List<T> HashTable D

  • C#实现自定义Dictionary类实例

    本文实例讲述了C#实现自定义Dictionary类.分享给大家供大家参考.具体如下: 1.关于MyDictionary类 本文中实现的MyDictionary类具有如下功能 1)可以增加.修改.删除键值对 2)可以通过索引器,找到一个键对应的值 3)可以遍历打印类中全部的键值对 4)可以将类中的序列转化为有序的(不排序.升序.降序)List类型 MyDictionary类是一个具有两个参数的泛型类,内部机制采用以键值对(KeyValuePair)类型为元素的双向链表(LinkedList)实现

  • C#中Dictionary泛型集合7种常见的用法

    要使用Dictionary集合,需要导入C#泛型命名空间 System.Collections.Generic(程序集:mscorlib)  Dictionary的描述 1.从一组键(Key)到一组值(Value)的映射,每一个添加项都是由一个值及其相关连的键组成 2.任何键都必须是唯一的 3.键不能为空引用null(VB中的Nothing),若值为引用类型,则可以为空值 4.Key和Value可以是任何类型(string,int,custom class 等) Dictionary常用用法:以

  • C#中Dictionary类使用实例

    在C#中,使用Dictionary类来管理由键值对组成的集合,这类集合称为字典. 字典最大的特点就是能够根据键来快速查找集合中的值. 下面是一个使用字典的小实例,希望通过这个小实例,能让大家对字典操作有一个初步的了解.下面是完整代码. //************************************************************ // // Dictionary示例代码 // // Author:三五月儿 // // Date:2014/09/14 // // //

  • C#创建安全的字典(Dictionary)存储结构

    在上面介绍过栈(Stack)的存储结构,接下来介绍另一种存储结构字典(Dictionary). 字典(Dictionary)里面的每一个元素都是一个键值对(由二个元素组成:键和值) 键必须是唯一的,而值不需要唯一的,键和值都可以是任何类型.字典(Dictionary)是常用于查找和排序的列表. 接下来看一下Dictionary的部分方法和类的底层实现代码: 1.Add:将指定的键和值添加到字典中. public void Add(TKey key, TValue value) { Insert(

  • C#泛型集合Dictionary<K,V>的使用方法

    1.要使用Dictionary集合,需要导入C#泛型命名空间 System.Collections.Generic(程序集:mscorlib) 2.描述 1).从一组键(Key)到一组值(Value)的映射,每一个添加项都是由一个值及其相关连的键组成 2).任何键都必须是唯一的 3).键不能为空引用null(VB中的Nothing),若值为引用类型,则可以为空值 4).Key和Value可以是任何类型(string,int,custom class 等) 3.创建及初始化 复制代码 代码如下:

  • 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

  • 浅谈TreeSet中的两种排序方式

    直接上代码: package exercise1; public class Person implements Comparable{ private int id; private String name; public Person(int id, String name) { super(); this.id = id; this.name = name; } public int getId() { return id; } public void setId(int id) { th

  • MySQL中一些鲜为人知的排序方式

    前言 ORDER BY 字段名 升序/降序,相信进来的朋友都认识这个排序语句,但遇到一些特殊的排序,单单使用字段名就无法满足需求了,下面给大家介绍几个我遇到过的排序方法: 一.准备工作 为了更好演示与理解,先准备一张学生表,加入编号.姓名.成绩三个字段,插入几条数据,如图: 二.条件排序 需求一:成绩从高到低进行排序 街边卖菜的阿姨都能敲,直接使用 ORDER BY examScore DESC 轻松完成了(如下左图). 需求二:成绩从高到低进行排序,并且没录入成绩的排在最前面 客户体验最重要,

  • C#使用struct类型作为泛型Dictionary<TKey,TValue>的键

    我们经常用简单数据类型,比如int作为泛型Dictionary<TKey,TValue>的key,但有时候我们希望自定义数据类型作为Dictionary<TKey,TValue>的key,如何做到? 如果我们想自定义一个struct类型作为key,就必须针对该struct定义一个实现IEqualityComparer<T>接口的比较类,实现该接口的2个方法:Equals()方法和GetHashCode()方法,前者用来比较两个key是否相等,后者用来获取key的哈希值.

  • spring data JPA 中的多属性排序方式

    目录 springdataJPA的多属性排序 第一步,引包 第二步,service方法代码 springdataJPA排序问题(orderby) spring data JPA的多属性排序 在此介绍我所用的一种方式: 第一步,引包 import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort.Order; 第二步,service方法代码 @Override     public P

  • mybatis中orderBy(排序字段)和sort(排序方式)引起的bug及解决

    目录 引言 问题叙述 下面尝试采用第二种方式 第三种方式 3.1 首先是什么都不传 3.2 传入排序字段oderBy 引言 记录一个mybatis实现动态字段的排序和动态的升降序问题 实现效果如下: 问题叙述 在这里无论使用postman是否传递sort的值, 都不生效, postman 执行的sql日志 ==>  Preparing: select sum(acd.read_view_count) as read_view_count,sum(acd.read_person_count) as

  • c# 遍历 Dictionary的四种方式

    一:背景 1. 讲故事 昨天在 StackOverflow 上看到一个很有趣的问题,说: 你会几种遍历字典的方式,然后跟帖就是各种奇葩的回答,挺有意思,马上就要国庆了,娱乐娱乐吧,说说这种挺无聊的问题

  • C#中Dictionary的作用及用法讲解

    Dictionary<string, string>是一个泛型 他本身有集合的功能有时候可以把它看成数组 他的结构是这样的:Dictionary<[key], [value]> 他的特点是存入对象是需要与[key]值一一对应的存入该泛型 通过某一个一定的[key]去找到对应的值 举个例子: 复制代码 代码如下: //实例化对象 Dictionary<int, string> dic = new Dictionary<int, string>(); //对象打

  • Python中dictionary items()系列函数的用法实例

    本文实例讲述了Python中dictionary items()系列函数的用法,对Python程序设计有很好的参考借鉴价值.具体分析如下: 先来看一个示例: import html # available only in Python 3.x def make_elements(name, value, **attrs): keyvals = [' %s="%s"' % item for item in attrs.items()] attr_str = ''.join(keyvals

  • MySQL中对查询结果排序和限定结果的返回数量的用法教程

    MySQL Order By 查询结果排序 ORDER BY SQL 语法中 ORDER BY 关键字用于对查询结果进行排序. 排序分为升序(ASC)和降序(DESC)两种,当不使用 ORDER BY 指定排序方式时,默认为升序. 语法: SELECT column,- FROM tb_name ORDER BY column1,column2,- DESC(ASC) ORDER BY 后面必须列出排序的字段名,可以是多个字段. 对 user 表 uid 进行降序查询: SELECT uid,u

随机推荐