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. mysql字符串比较

    select '123'B is TRUE;  1 SET @a='123';            select '123'is TRUE;      0 select cast('222' as ...

  2. X11 FRAMEBUFFER QT

    之前对X11 FRAMEBUFFER理解的不够,现在总结一下Qt Embedded是挪威Trolletch公司的图形化界面开发工具Qt的嵌入式版本,它通过QtAPI与LinuxI/O以及Framebu ...

  3. Windows2003 Webshell默认权限

    0×00 前言 0×01 Windows2003默认配置 0×02 Windows2003典型配置的权限 0×03 cmd运行的条件   0×00 前言 这一章主要讲解关于我们刚拿到webshell的 ...

  4. SQL INTERSECT

    SQL INTERSECT is query that allows you to select related information from 2 tables, this is combine ...

  5. 【转】TCP分段与IP分片

    原文: :https://www.jianshu.com/p/f9a5b07d99a2 -------------------------------------------------------- ...

  6. WordPress 如何修改编辑器TinyMCE里的内容

    //获取编辑器对象,wp中的编辑器ID是"content" var editor = tinymce.get('content'); //获取编辑器内容 var content = ...

  7. 北京SAP-AGS CoE support consultant intern 面试总结

    7月4号去大连SAP面试.通过的话直接去北京SAP參加实习(有幸已通过). SAP一直是我理想的公司,大二的时候曾试图參加SAP大连vt项目,当时下了非常大决心,假设通过了就先留在大连,可惜英语群面就 ...

  8. python的加密模块(md5,sha,crypt)学习

    python的加密模块(md5,sha,crypt)学习 命令行使用python MD5: yinguicai@Cpl-IBP-Product:~/data/work/svn/v1.4.0_dev/A ...

  9. es5 - array - push

    /** * 参数:arr.push(element1, ..., elementN) * 作用:被添加到数组末尾的元素. * 当调用该方法时,新的 length 属性值将被返回. * 描述:push ...

  10. Minimum edit distance(levenshtein distance)(最小编辑距离)初探

    最小编辑距离的定义:编辑距离(Edit Distance),又称Levenshtein距离.是指两个字串之间,由一个转成还有一个所需的最少编辑操作次数.许可的编辑操作包含将一个字符替换成还有一个字符. ...