C# SortedDictionary<TKey,TValue>排序 用法 Sort()用法
2013-07-23 21:14
288 查看
使用过Dictionary的人都知道,当每一个Add里面的值都不会改变其顺序,所以需要需要对其排序的时候就用到SortedDictionary,但SortedDictionary并不是那么理想,其默认的方式只支持正序排序,想要反序排序时必须得靠自己重新编写代码,下面来看一个简单的例子:
如以下代码在调试时不能使用则需要引用:
using System.Linq;
using System.Collections.Generic;
上面代码输出效果:
键名:1123 键值:lslgsgl
键名:2bcd13 键值:value
键名:321 键值:fdsgsags
键名:acb 键值:test test
好了,现在我们来看一下反序排序的效果,请看下面的代码:
上面代码输出效果:
正序排序数据:
键名:1123 键值:lslgsgl
键名:2bcd13 键值:value
键名:321 键值:fdsgsags
键名:acb 键值:test test
反序排序数据:
键名:acb 键值:test test
键名:321 键值:fdsgsags
键名:2bcd13 键值:value
键名:1123 键值:lslgsgl
具有相同的Key时的排序。Value是相同key的集合
internal List<BoxDetailInfo> MergeListBySkuUdf1_AiWei(List<BoxDetailInfo> boxDetailList)
{
var sortDictionary = new SortedDictionary<string, List<BoxDetailInfo>>();
foreach (var boxDetailInfo in boxDetailList)
{
if (sortDictionary.ContainsKey(boxDetailInfo.skuUdf1))
{
sortDictionary[boxDetailInfo.skuUdf1].Add(boxDetailInfo);
}
else
{
sortDictionary.Add(boxDetailInfo.skuUdf1, new List<BoxDetailInfo>() { boxDetailInfo });
}
}
var dictionaryList = new List<BoxDetailInfo>();
foreach (List<BoxDetailInfo> pair in sortDictionary.Values)
{
foreach (BoxDetailInfo DetailInfo in pair)
{
dictionaryList.Add(DetailInfo);
}
}
return dictionaryList;
}
Sort()排序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 测试
{
class Program
{
static void Main(string[] args)
{
List<String> list = new List<string>();
list.Add("-1");
list.Add("-11");
list.Add("2");
list.Add("4");
list.Add("110");
list.Add("-29");
list.Add("10");
list.Sort(MyCompareString);
foreach (string a in list )
{
Console.WriteLine("{0}", a);
}
}
private static int MyCompareString(string x, string y)
{
return int.Parse(y).CompareTo(int.Parse(x));
}
}
}
如以下代码在调试时不能使用则需要引用:
using System.Linq;
using System.Collections.Generic;
1 private void TestDictionarySort() 2 { 3 SortedDictionary<string, string> sd = new SortedDictionary<string, string>(); 4 sd.Add("321", "fdsgsags"); 5 sd.Add("acb", "test test"); 6 sd.Add("1123", "lslgsgl"); 7 sd.Add("2bcd13", "value"); 9 10 foreach (KeyValuePair<string, string> item in sd) 11 { 12 Response.Write("键名:" + item.Key + " 键值:" + item.Value); 13 } 14 15 }
上面代码输出效果:
键名:1123 键值:lslgsgl
键名:2bcd13 键值:value
键名:321 键值:fdsgsags
键名:acb 键值:test test
好了,现在我们来看一下反序排序的效果,请看下面的代码:
private void TestDictionarySort() { SortedDictionary<string, string> sd = new SortedDictionary<string, string>(); sd.Add("321", "fdsgsags"); sd.Add("acb", "test test"); sd.Add("1123", "lslgsgl"); sd.Add("2bcd13", "value"); Response.Write("<br />正序排序数据:<br />"); foreach (KeyValuePair<string, string> item in sd) { Response.Write("键名:" + item.Key + " 键值:" + item.Value + "<br />"); } //重新封装到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; //再看其输出结果: Response.Write("<br />反序排序数据:<br />"); foreach (KeyValuePair<string, string> item in dc) { Response.Write("键名:" + item.Key + " 键值:" + item.Value + "<br />"); } }
上面代码输出效果:
正序排序数据:
键名:1123 键值:lslgsgl
键名:2bcd13 键值:value
键名:321 键值:fdsgsags
键名:acb 键值:test test
反序排序数据:
键名:acb 键值:test test
键名:321 键值:fdsgsags
键名:2bcd13 键值:value
键名:1123 键值:lslgsgl
具有相同的Key时的排序。Value是相同key的集合
internal List<BoxDetailInfo> MergeListBySkuUdf1_AiWei(List<BoxDetailInfo> boxDetailList)
{
var sortDictionary = new SortedDictionary<string, List<BoxDetailInfo>>();
foreach (var boxDetailInfo in boxDetailList)
{
if (sortDictionary.ContainsKey(boxDetailInfo.skuUdf1))
{
sortDictionary[boxDetailInfo.skuUdf1].Add(boxDetailInfo);
}
else
{
sortDictionary.Add(boxDetailInfo.skuUdf1, new List<BoxDetailInfo>() { boxDetailInfo });
}
}
var dictionaryList = new List<BoxDetailInfo>();
foreach (List<BoxDetailInfo> pair in sortDictionary.Values)
{
foreach (BoxDetailInfo DetailInfo in pair)
{
dictionaryList.Add(DetailInfo);
}
}
return dictionaryList;
}
Sort()排序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 测试
{
class Program
{
static void Main(string[] args)
{
List<String> list = new List<string>();
list.Add("-1");
list.Add("-11");
list.Add("2");
list.Add("4");
list.Add("110");
list.Add("-29");
list.Add("10");
list.Sort(MyCompareString);
foreach (string a in list )
{
Console.WriteLine("{0}", a);
}
}
private static int MyCompareString(string x, string y)
{
return int.Parse(y).CompareTo(int.Parse(x));
}
}
}
相关文章推荐
- C# SortedDictionary<TKey,TValue>排序 用法 Sort()用法
- [C#]泛型与非泛型集合类的区别及使用例程,包括ArrayList,Hashtable,List<T>,Dictionary<Tkey,Tvalue>,SortedList<Tkey,Tvalue>,
- c# 泛型集合Dictionary<TKey,TValue>
- C# 谈Dictionary<TKey,TValue>,SortedDictionary<TKey,TValue>排序
- C# KeyValuePair<TKey,TValue>的用法【转】
- C# Dictionary的用法 KeyValuePair<TKey, TValue> 结构
- C# 中 List.Sort运用(IComparer<T>)排序用法
- c# 泛型集合Dictionary&lt;TKey,TValue&gt;
- .Net集合类的研究-有序集合(二)-SortedDictionary<TKey,TValue>
- C#中List<T>的排序用法(Sort)
- C# List<T>.Sort排序
- C#基础---IComparable用法,实现List<T>.sort()排序
- C#高级编程五十三天----字典Dictionary<TKey,TValue>
- [C#]泛型与非泛型集合类的区别及使用例程,包括ArrayList,Hashtable,List<T>,Dictionary<Tkey,Tvalue>,SortedList<Tkey,Tvalue>,Queue<T>,Stack<T>等
- C#基础---IComparable用法,实现List<T>.sort()排序
- [C#基础知识]泛型Dictionary<string,string>的用法详解
- .Net集合类的研究-有序集合(一)-SortedList、SortedList<TKey,TValue>
- SortedDictionary<TKey,TValue>正序与反序排序
- C# 中 List.Sort运用(IComparer<T>)排序用法
- C#基础教程之IComparable用法,实现List<T>.sort()排序