订购词典

发布于 2024-12-01 23:05:25 字数 1774 浏览 1 评论 0原文

我使用以下代码将信息添加到字典中:

foreach (string word in lineList)
{
    if (dictionary.ContainsKey(word))
        dictionary[word]++;
    else
        dictionary[word] = 1;
}

// I believe this is what needs to change..?
var ordered = from k in dictionary.Keys select k;

当我使用 StreamWriter 打印行时,它会按照添加到字典中的顺序打印出来。

我想做的是按照首先比较 PartDescription 然后比较 PartNumber 的顺序打印出来,然后以数字方式打印出来。

文件如下所示:

PartDescription        PartNumber        Name        X        Y        Rotation
1608RTANT              147430            J1        20.555   -12.121       180
TANTD                  148966            J2        20.555   -12.121       270
SOMETHING              148966            R111      20.555   -12.121       360
SOMETHING              148966            C121      20.555   -12.121       180
SOMETHING              148966            R50       205.555  -12.121       180
SOMETHING              148966            R51      -205.555  125.121       270
SOMETHING              148966            R52       20.555   -12.121       0
SOMETHING              148966            C12       20.555   -12.121       0
1709RTANT              147430            C98       20.555   -12.121       0
1608RTANT              147429            QD1       20.555   -12.121       180
1709RTANT              147430            F12       20.555   -12.121       0
1609RTANT              147445            P9        20.555   -12.121       180

StreamWriter 将输出如下:

1, 1608RTANT, 147429, 1   //Line#, PartDescription, PartNumber, # of Duplicates (from dictionary key)
2, 1608RTANT, 147430, 1
3, 1609RTANT, 147445, 1
4, 1709RTANT, 147430, 2
5, SOMETHING, 148966, 6
6, TANTD, 148966, 1

I am adding information to a dictionary using this code:

foreach (string word in lineList)
{
    if (dictionary.ContainsKey(word))
        dictionary[word]++;
    else
        dictionary[word] = 1;
}

// I believe this is what needs to change..?
var ordered = from k in dictionary.Keys select k;

When I use the StreamWriter to print out the lines it is printing it out in the order it was added to the dictionary.

What I am trying to do is print it out in an order that first compares the PartDescriptionand then the PartNumber and prints it out numerically.

the File looks like this:

PartDescription        PartNumber        Name        X        Y        Rotation
1608RTANT              147430            J1        20.555   -12.121       180
TANTD                  148966            J2        20.555   -12.121       270
SOMETHING              148966            R111      20.555   -12.121       360
SOMETHING              148966            C121      20.555   -12.121       180
SOMETHING              148966            R50       205.555  -12.121       180
SOMETHING              148966            R51      -205.555  125.121       270
SOMETHING              148966            R52       20.555   -12.121       0
SOMETHING              148966            C12       20.555   -12.121       0
1709RTANT              147430            C98       20.555   -12.121       0
1608RTANT              147429            QD1       20.555   -12.121       180
1709RTANT              147430            F12       20.555   -12.121       0
1609RTANT              147445            P9        20.555   -12.121       180

The StreamWriter would output like this:

1, 1608RTANT, 147429, 1   //Line#, PartDescription, PartNumber, # of Duplicates (from dictionary key)
2, 1608RTANT, 147430, 1
3, 1609RTANT, 147445, 1
4, 1709RTANT, 147430, 2
5, SOMETHING, 148966, 6
6, TANTD, 148966, 1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把梦留给海 2024-12-08 23:05:25

好吧,你当然可以轻松地以有序的方式获取键:

var ordered = from k in dictionary.Keys orderby k select k;

或者更简单:

var ordered = dictionary.Keys.OrderBy(x => x);

请注意,你不应该依赖字典按照你添加它们的顺序存储对 - 基本上你应该不假设字典中有任何排序。

Well you could certainly get the keys in an ordered way easily:

var ordered = from k in dictionary.Keys orderby k select k;

Or even more simply:

var ordered = dictionary.Keys.OrderBy(x => x);

Note that you shouldn't rely on the dictionary storing the pairs in the order in which you added them - basically you shouldn't assume any ordering out of the dictionary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文