简体   繁体   English

项目以错误的顺序添加到排序列表中

[英]Items being added into sorted list in wrong order

I'm adding a list of values into a sorted list. 我将值列表添加到排序列表中。 The problem is that it's not adding the data into my sorted list correctly. 问题在于它没有将数据正确添加到我的排序列表中。

public static void newList(List<string> oldList, List<string> headers)
    {
        var noHeaders = removeHeaders(oldList, headers);

        SortedList<string, string> newList = new SortedList<string, string>();
        for (int i = 0; i < headers.Count; i++)
        {
            newList.Add(headers[i], "placeholder");
        }

            //foreach (string header in headers)
            //{
            //    newList.Add(header, "placeholder");
            //}

Above is a snippet of my code, I've tried all different kinds of loops with the same result. 上面是我的代码段,我尝试了所有不同类型的循环,但结果相同。

The data from the "headers" list looks like this: 来自“标头”列表的数据如下所示:

[0]Value1
[1]Value2
[2]Value3
[3]Value4

When I look at the same data once it's been added into the sorted list it looks like this: 将相同的数据添加到排序后的列表中后,我将看到以下数据:

[0]Value2
[1]Value1
[2]Value4
[3]Value3

Can anybody explain to me why this is happening so that I can fix it? 有人可以向我解释为什么会这样,以便我解决它吗? I've searched around for a bit but couldn't find anything helpful. 我搜索了一下,但找不到任何帮助。

Thanks 谢谢

According to this documentation , the entries of the list are sorted with respect to the keys , not the values ; 根据该文档 ,列表的条目是根据而不是排序的; this means that the order you observe depends on the values provided in headers and the values as such are not sorted. 这意味着您观察到的顺序取决于headers提供的 ,因此这些不会排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM