【问题标题】:How To sort Dictionary After Adding data to it (C#)向字典添加数据后如何对字典进行排序(C#)
【发布时间】:2021-11-18 13:52:37
【问题描述】:

大家好,我的问题是当我在 c# 中将数据添加到 Dictionary 并且我想切换(Sortype)字符串以检查用户如何想要订单数据时 像这样:

                         string SortType="ByDownloads";
                         Dictionary<string, Data> WorldInfo = JsonConvert.DeserializeObject<Dictionary<string, Data>>(Res.Body.ToString());
                            switch (SortType)
                            {
                                case "ByDownloads":
                                    WorldInfo.OrderByDescending(AllData => AllData.Value.PostDownloads).ToList();
                                    break;
                                case "ByViews":
                                    WorldInfo.OrderBy(AllData => AllData.Value.PostViews).ToList();
                                    break;
                            }

这样,代码将不起作用,数据没有排序 但是当我使用这种方式时,代码将起作用:

Dictionary<string, Data> WorldInfo = JsonConvert.DeserializeObject<Dictionary<string, Data>>(Res.Body.ToString());
var NewSortedDictionary = WorldInfo.OrderByDescending(AllData => AllData.Value.PostViews).ToList();

所以我正在寻找正确的方法来执行此操作并使用 (WorldInfo) 字典并切换 (SortType) 然后我使用它而不是 (NewSortedDictionary)。 谢谢你:)

【问题讨论】:

  • 但是.. 您将 Sortype 设置为一个不会出现在 switch 语句中的值
  • 感谢您的回复,我有些地方错了,我更新了代码看看问题出在哪里
  • 避免混淆 - 你不能对字典进行排序 - 你正在做的是枚举和排序字典的内容并存储到键值对列表中并将其称为字典;不是,所以你不应该
  • 字典未排序。你是说 SortedList 吗?

标签: c# .net dictionary


【解决方案1】:

感谢所有对此问题发表评论的人:) 我用这种方式修复了它,我希望它对将来的人有所帮助:

            SortType="ByDownloads";
            Dictionary<string, Data> NormallDictionary = JsonConvert.DeserializeObject<Dictionary<string, Data>>(Res.Body.ToString());
            List<KeyValuePair<string,Data>> MySortedDictionary=  new List<KeyValuePair<string, Data>>();
            switch (SortType)
            {
                case "ByDownloads":
                MySortedDictionary = NormallDictionary.OrderByDescending(x => x.Value.Downloads).ToList();
                break;
                case "ByViews":
                MySortedDictionary = NormallDictionary.OrderByDescending(x => x.Value.Views).ToList();
                break;
            }

现在您可以随意使用 MySortedDictionary

【讨论】:

    【解决方案2】:

    在您的情况下它不起作用的原因是 OrderByDescending 在订购后返回一个对象。

    你可以这样做

            Dictionary<string, Data> MyDictionary = JsonConvert.DeserializeObject<Dictionary<string, Data>>(Res.Body.ToString()));
            var data = Sortype == "ByViews" ? WorldInfo.OrderByDescending(AllData => AllData.Value.WorldPDownloads).ToList() : Sortype == 'ByDownloads' ? WorldInfo.OrderBy(AllData => AllData.Value.WorldPDownloads).ToList() : null;
    

    或者

            string Sortype = "ByDate";
            Dictionary<string, Data> MyDictionary = JsonConvert.DeserializeObject<Dictionary<string, Data>>(Res.Body.ToString());
            switch (Sortype)
            {
                case "ByViews":
                    WorldInfo = WorldInfo.OrderByDescending(AllData => AllData.Value.WorldPDownloads).ToList();
                    break;
                case "ByDownloads":
                    WorldInfo =WorldInfo.OrderBy(AllData => AllData.Value.WorldPDownloads).ToList();
                    break;
            }
    

    【讨论】:

    • 感谢您的回复 :),我在某些事情上错了,我更新了代码以查看问题出在哪里,我只想使用 (WorldInfo) 字典并切换排序类型,然后我使用它而不是(NewSortedDictionary)
    • 您能否详细说明您现在正在做什么以及您期望什么?
    • @CaptainPrice 如果您认为这个答案是正确的,也请标记它。未来的用户可能会从中受益
    • 感谢您对这个问题的关注,我已修复,祝您生活愉快:)
    【解决方案3】:

    我不知道这是一个错误还是只是故意遗漏,但您设置 string Sortype ="ByDate"; 而实际上在 switch 语句中没有 "ByDate"

    【讨论】:

    • 感谢您的回复,(ByDate)它只是数据类中的一个公共字符串,但是当我尝试对字典进行排序时出现问题
    • 作为临时解决方案,您可以尝试在订购前将数据提取到单独的Listvar tempList = WorldInfo.Select(AllData =&gt; AllData.Value.PostDownloads).ToList(); tempList.OrderByDescending(o =&gt; o); 话虽如此,奇怪的是您的方法不起作用,输入和预期的错误输出是什么?
    • 感谢您的回复,我做了一些更改,现在可以使用了 :),祝您编码愉快
    猜你喜欢
    • 2018-09-27
    • 1970-01-01
    • 2016-05-30
    • 2014-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-26
    相关资源
    最近更新 更多