0

Hello Everyone my problem is when i add the data to Dictionary in c# and i want to switch (Sortype) string to check how the user wants the order data like this:

                         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;
                            }

In this way, the code Will Not work and the Data does not sorted but when i use this way the code will work:

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

So I'm looking for the right way to do it and use (WorldInfo) Dictionary and switch (SortType) then i use it instead of (NewSortedDictionary). Thank you :)

4
  • 1
    But.. you set Sortype to a value that doesn't then appear in the switch statement Commented Sep 25, 2021 at 19:19
  • Thanks for your reply, I was wrong about some things, I updated the code to see where the problem is Commented Sep 25, 2021 at 20:53
  • 1
    Avoid confusing yourself - you cannot sort a Dictionary<T,U> - what you're doing is enumerating and sorting the content of the dictionary and storing into a list of key value pairs and calling it a dictionary; it isn't, so you shouldn't Commented Sep 25, 2021 at 21:58
  • Dictionaries are not sorted. Do you mean SortedList? Commented Sep 26, 2021 at 4:58

3 Answers 3

1

Thanks For Everyone Who Comment on this Question :) i fixed it using this way,i hope it help someone in the future:

            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;
            }

Now You Can Use MySortedDictionary as You Want

Sign up to request clarification or add additional context in comments.

Comments

0

The reason it does not work in your case is because OrderByDescending returns an object after ordering.

You can do something like this

        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;

Or

        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;
        }

4 Comments

Thanks for your reply :), I was wrong about some things, I updated the code to see where the problem is i just want to use (WorldInfo) Dictionary and switch sorttype then i use it instead of (NewSortedDictionary)
Can you elaborate more on what you are doing now and what do you expect?
@CaptainPrice If you think this answer was correct, mark it as well. future users might benefit from this
Thank you for your attention to this issue, I fixed it I wish you a happy life :)
0

I do not know if that is a bug or just missing on purpose but you are setting string Sortype ="ByDate"; without actually having a "ByDate" in the switch statement.

3 Comments

Thanks for your reply , (ByDate) its just a public string in Data Class but the problem when i try to sort Dictionary
As a temporary solution you could try extracting the data into a separate List before ordering: var tempList = WorldInfo.Select(AllData => AllData.Value.PostDownloads).ToList(); tempList.OrderByDescending(o => o); That being said, it is strange that you method does not work, what exactly is the input and what is the expected incorrect output?
Thank you for your response, I made some changes and it works now :) , happy coding

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.