对字典集合进行排序 [英] Sorting on dictionary collection

查看:89
本文介绍了对字典集合进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何对字典值集合进行排序,如值为4 5/8

 6 3/4 
9 3/4
11 1 / 4
15 1/4
8 1/2
11 7/8
14 3/4
16
19 5/8
13 1/2
7
15
23 3/4
7 3/4
11 3/8
12 1/2
7 1/8
16 1/16
12
9
5 1/2



提前致谢。

解决方案

这些不是字典条目:字典是一个特定的集合,它有一个Key(必须是唯一的)和一个Value(不需要be)。



字典通常不是排序的:它作为哈希表工作,因此按键或值排序会降低访问速度。但是有一个SortedDictionary类按键排序,但检索比标准Dictionary慢,因为它存储为列表,而不是哈希表。



但是,如果您有一个包含分数字符串值的列表,并且您想要对其进行排序,则需要先将它们转换为数值:

  private   void  myButton_Click( object  sender,EventArgs e) 
{
List< string> fractions = new List< string>(){
6 3/4 9 3/4 11 1/4 15 1/4 8 1/2
11 7/8 14 3/4 16 19 5/8 13 1/2 7};
List< string> sortedFractions = fractions.OrderBy(f = > ToValue(f))。ToList();
foreach string s in sortedFractions)
{
Console.WriteLine(s);
}
}
私有 静态 double ToValue( string fraction)
{
double result = 0 0 ;
string [] parts = fraction.Split( new char [] {' '},StringSplitOptions.RemoveEmptyEntries);
if (parts.Length > 0
{
result = double .Parse(parts [ 0 ]);
if (parts.Length > 1
{
parts = parts [ 1 ]。分割( new char [] {' /'},StringSplitOptions.RemoveEmptyEntries);
if (parts.Length == 2
result + = double .Parse(parts [ 0 ])/ double .Parse(parts [ 1 ]);
}
}
返回结果;
}


您好,



请参阅以下链接。它可能对您有所帮助..



http:// www。 dotnetperls.com/sort-dictionary [ ^ ]

How to sort dictionary values collection like values are 4 5/8

6  3/4
9  3/4
11 1/4
15 1/4
8  1/2
11 7/8
14 3/4
16
19 5/8
13 1/2
7
15
23 3/4
7  3/4
11 3/8
12 1/2
7  1/8
16 1/16
12
9
5  1/2


Thanks in advance .

解决方案

Those aren't Dictionary entries: a Dictionary is a specific collection that has a Key (which must be unique) and a Value (which doesn't have to be).

Dictionaries are not normally sorted: it works as a Hash Table, so sorting by key or value would slow down access. There is however a SortedDictionary class that does sort by Key, but retrieval is slower than a standard Dictionary as it is stored as a list, rather than a Hash Table.

If, however, you have a list containing string values of fractions, and you want to sort that, you need to convert them to a numeric value first:

private void myButton_Click(object sender, EventArgs e)
    {
    List<string> fractions = new List<string>() {
            "6 3/4", "9 3/4", "11 1/4", "15 1/4", "8 1/2",
            "11 7/8","14 3/4","16","19 5/8","13 1/2","7"};
    List<string> sortedFractions = fractions.OrderBy(f => ToValue(f)).ToList();
    foreach (string s in sortedFractions)
        {
        Console.WriteLine(s);
        }
    }
private static double ToValue(string fraction)
    {
    double result = 0.0;
    string[] parts = fraction.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
    if (parts.Length > 0)
        {
        result = double.Parse(parts[0]);
        if (parts.Length > 1)
            {
            parts = parts[1].Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
            if (parts.Length == 2)
            result += double.Parse(parts[0]) / double.Parse(parts[1]);
            }
        }
    return result;
    }


Hi,

Please see the below link. It might be helpful to you..

http://www.dotnetperls.com/sort-dictionary[^]


这篇关于对字典集合进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆