using Except in sorting of dictionary

ankit goel 766 Reputation points
2022-12-20T11:11:21.547+00:00

@Jack J Jun , After creating a dictionary with the below declaration , i am sorting it in the descending order according to the number of values each keys hold .

   Dictionary<string, List<string>> dic = new Dictionary<string, List<string>>(StringComparer.CurrentCultureIgnoreCase);  

// insert some data here

           dic = dic.OrderByDescending(d => d.Value.Count).ToDictionary(x => x.Key, x => x.Value);    

Now, what I am trying to do is, exclude this sorting for some strings, from the list of string in the above same query. Can you please suggest me how to achieve the same in this query only.

Developer technologies C#
{count} votes

Accepted answer
  1. Jack J Jun 25,296 Reputation points
    2022-12-21T01:58:37.267+00:00

    @ankit goel , Welcome to Microsoft Q&A, you could try to use the Enumerable.Except Method to exclude your unwanted strings.

    Here is a code example you could refer to.

     OrderedDictionary count = new OrderedDictionary();  
            count.Add("Other Model", 22);  
            count.Add("Infinx", 12);  
            count.Add("Apple", 23);  
            count.Add("Samsung", 23);  
            count.Add("test", 45);  
            count.Add("nokia", 34);  
            List<string> list = new List<string>();  
            list.Add("Samsung");  
            list.Add("Infinx");  
            list.Add("Other Model");  
            list.Add("Apple");  
            var exlist = new List<string>();  
            exlist.Add("Infinx");  
            list = list.Except(exlist).ToList();  
            foreach (var item in list)  
            {  
                foreach (var kvp in count.Cast<DictionaryEntry>().Reverse().Reverse())  
                {  
                    if(item==kvp.Key.ToString())  
                    {  
                        count.Remove(kvp.Key);  
                        count.Insert(list.IndexOf(item), kvp.Key, kvp.Value);  
                    }  
                }  
            }  
    

    Result:

    274261-image.png

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2022-12-20T18:40:37.537+00:00

    in the ToDictionary, pass your own compare method as the third parameter. your method will need to know how to sort the desired excluded strings. do they come first, last, middle, etc. also what order should they be in?

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.