SortedDictionary<TKey,TValue>能对字典排序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace SortDictionary
{
class Program
{
static void Main(string[] args)
{
TestDictionarySort();
TestDictionarySort2();
Console.Read();
}
private static void TestDictionarySort()
{
SortedDictionary<string, string> sd = new SortedDictionary<string, string>();
sd.Add("", "fdsgsags");
sd.Add("acb", "test test");
sd.Add("", "lslgsgl");
sd.Add("2bcd13", "value"); foreach (KeyValuePair<string, string> item in sd)
{
Console.Write("键名:" + item.Key + " 键值:" + item.Value+"\r\n");
} } private static void TestDictionarySort2()
{
SortedDictionary<string, string> sd = new SortedDictionary<string, string>();
sd.Add("", "fdsgsags");
sd.Add("acb", "test test");
sd.Add("", "lslgsgl");
sd.Add("2bcd13", "value"); Console.Write("\r\n正序排序数据:\r\n");
foreach (KeyValuePair<string, string> item in sd)
{
Console.Write("键名:" + item.Key + " 键值:" + item.Value + "\r\n");
} //重新封装到Dictionary里(PS:因为排序后我们将不在使用排序了,所以就使用Dictionary)
Dictionary<string, string> dc = new Dictionary<string, string>();
foreach (KeyValuePair<string, string> item in sd.Reverse())
{
dc.Add(item.Key, item.Value);
}
sd = null;
//再看其输出结果:
Console.Write("\r\n反序排序数据:\r\n");
foreach (KeyValuePair<string, string> item in dc)
{
Console.Write("键名:" + item.Key + " 键值:" + item.Value + "\r\n");
}
} }
}

结果:

通过字典key得到value

var keywordDic = new Dictionary<int, string>()
{
{0,"搜索关键字"},
{1,"分类id"},
{2,"品牌id"}
};
var keywordCode = keywordDic[(int)item.KeyWordType];

Listl转Dictionary

        public Dictionary<int?, string> GetForbiddenTypeList()
{
//var dic = new Dictionary<int?, string>();
var list = new List<ForbiddenTypeDetail>();
var result = BSClient.Send<ForbiddenTypeResponse>(new ForbiddenTypeRequest());
if (result.DoFlag)
{
//foreach (var item in result.ForbiddenType)
//{
// if (!string.IsNullOrEmpty(item.Type) && item.Id.HasValue)
// dic.Add(item.Id, item.Type);
//} list = Mapper.MappGereric<ForbiddenType, ForbiddenTypeDetail>(result.ForbiddenType).ToList();
}
return list.Where(item => (!string.IsNullOrEmpty(item.Type) && item.Id.HasValue)).ToDictionary(item => item.Id, item => item.Type);
//return dic;
}

todictionary:

var moduleDict = adListRes.ReturnValue.AdModuleDataDto.Where(itemlist => itemlist.Data.ToList().Count > 0).ToDictionary
(itemlist => itemlist.ModuleCode, itemlist => itemlist.Data.ToList())

SortedDictionary<TKey,TValue>正序与反序排序及Dicttionary相关的更多相关文章

  1. SortedDictionary<TKey, TValue> 类 表示根据键进行排序的键/值对的集合。

    SortedDictionary<TKey, TValue> 类   表示根据键进行排序的键/值对的集合. SortedDictionary<TKey, TValue> 中的每 ...

  2. .net学习笔记----有序集合SortedList、SortedList<TKey,TValue>、SortedDictionary<TKey,TValue>

    无论是常用的List<T>.Hashtable还是ListDictionary<TKey,TValue>,在保存值的时候都是无序的,而今天要介绍的集合类SortedList和S ...

  3. C# 谈Dictionary<TKey,TValue>,SortedDictionary<TKey,TValue>排序

    使用过Dictionary的人都知道,当每一个Add里面的值都不会改变其顺序,所以需要需要对其排序的时候就用到SortedDictionary, 但SortedDictionary并不是那么理想,其默 ...

  4. 找一个四位数,要求该四位数的四倍刚好是该四位数的反序。 即b1b2b3b4 * 4 = b4b3b2b1

    找一个四位数,要求该四位数的四倍刚好是该四位数的反序. 即b1b2b3b4 * 4 = b4b3b2b1 解: 第一步,确认最末位 假设 b1b2b3b4 + b4b3b2b1 = [x0]x1x2x ...

  5. Django的列表反序

    Django虽然是python的web框架,但它不是所有的python特性都支持的. 最近在项目中遇到一个问题,需要在Django中将获得的列表反序排列,一开始我使用的是python的reverse方 ...

  6. leetcode:Reverse Integer(一个整数反序输出)

    Question:Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...

  7. pojg2744找一个最长的字符串x,使得对于已经给出的字符串中的任意一个y,x或者是y的子串,或者x中的字符反序之后得到的新字符串是y的子串。

    http://poj.grids.cn/practice/2744 描述现在有一些由英文字符组成的大小写敏感的字符串,你的任务是找到一个最长的字符串x,使得对于已经给出的字符串中的任意一个y,x或者是 ...

  8. 前端总结·基础篇·JS(二)数组深拷贝、去重以及字符串反序和数组(Array)

    目录 这是<前端总结·基础篇·JS>系列的第二篇,主要总结一下JS数组的使用.技巧以及常用方法. 一.数组使用 1.1 定义数组 1.2 使用数组 1.3 类型检测 二.常用技巧 2.1 ...

  9. 编写一个类,其中包含一个排序的方法Sort(),当传入的是一串整数,就按照从小到大的顺序输出,如果传入的是一个字符串,就将字符串反序输出。

    namespace test2 { class Program { /// <summary> /// 编写一个类,其中包含一个排序的方法Sort(),当传入的是一串整数,就按照从小到大的 ...

随机推荐

  1. 关闭OOMER

    http://www.cnblogs.com/itfriend/archive/2011/12/14/2287160.html http://blog.csdn.net/gugemichael/art ...

  2. HDU 2896 病毒侵袭 【AC自动机】

    HDU 2222 仅仅求出了和文本串匹配的模式串个数,本题要求求出匹配的模式串的编号. 不同的部分在代码中的注释部分. #include <cstdio> #include <cst ...

  3. 【java失业择业中】失业第四天:准备面试

    1.jQuery基础 学好jquery的一个基础条件是学好css层叠样式,因为很多时候这2个是一块配合使用的. 页面中很多需要jquery实现的效果只是通过jquery的选择器,选中要操作的元素,添加 ...

  4. 一些值得学习的Unity教程 (很实用的包括源码)

    ***********************项目源码******************************** 1. 降临 2. 沉睡缤纷乐 3. 千炮捕鱼 4. Photon官方FSP示例 ...

  5. easyui 隔行渐变色属性设置

    1.部分JS文件 function getAgencyDealTable(begin,end,regionFlag,agencyId){ $('#reportList').datagrid({ idF ...

  6. 批量删除linux的文件;find方法批量删除文件;find查找某时间段内的所有文件

    1.如图所示,有大量文件夹,想批量删除它们 2.使用命令 find . -maxdepth 1  -regex ".*ws.*" 可以批量找到他们.maxdepth值为1表示只在当 ...

  7. Android 使用SWIG生成Jni代码

    一,下载SWIG,并设置好环境变量 二以HelloJni为例子 导入ndk sample的HelloJni工程. 三 创建SWIG接口文件 在jni目录下新建文件Unix.i,输入SWIG代码: %m ...

  8. Backup and restore of FAST Search for SharePoint 2010

    一个同事问我一个问题: 如果FAST Search for SharePoint 2010被full restore到了一个之前的时间点, 那么当FAST Search重新开始一个增量爬网的时候, 会 ...

  9. 如何在Windows Server 2012 R2上安装SharePoint 2013

    笔者原以为是个挺容易个事儿, 毕竟是微软自家的产品安装在自家的操作系统上, 没想到还是让我费了半天劲.   写在这里吧, 方便其他的朋友.   具体步骤 ======================= ...

  10. EChart处理三维数据做图表、多维legend图例处理

    处理三维数据做图表,比如返回的数据就是一个个list,list里面某几个数据同属于一个维度,项目中的实例效果如下: 上面的khfx会有多个,比如db1.db2.db3等,下面的那些数据也会变化,目前需 ...