0

I have a dictionary:

Dictionary<char, Dictionary<char, int>> vertices = new Dictionary<char, Dictionary<char, int>>();

Example when adding:

('A', new Dictionary<char, int>() { { 'B', 5 }, { 'C', 3 }, { 'E', 3}, { 'F', 1 } });

Iam passing the Dictionary<char, int>to another class where I put all the int values from the dictionary to an array so that they can be sorted(heapsort).

Before:

{ 'B', 5 }, { 'C', 3 }, { 'E', 3}, { 'F', 1 } }

After:

[1,3,3,5}

How can I re-assign the values so that the keys are also re-arranged?

I want :

('A', { 'F', 1 }, { 'C', 3 }, { 'E', 3 }, { 'B',5 });

EDIT: Heap sort

 List<Edges> elements = new List<Edges>();
        List<int> r;
        private int heapSize;

        public void Add(Edges E)
        {
            elements.Clear();
            elements.Add(E);
            r =new List<int>();
            foreach (var value in elements)
            {
                var x = (value as Edges).Value as Dictionary<char, int>;
                foreach (var getValue in x.Values)
                {
                    r.Add(getValue);
                }
            }
            PerformHeapSort(r.ToArray());
        }
NOSMILE
  • 13
  • 1
  • 9
  • Is there any particular reason for sorting the values first and then trying to reassign the keys? If not then [here](https://stackoverflow.com/questions/289/how-do-you-sort-a-dictionary-by-value) is a link for an SO post that describes a way to sort a dictionary by value, by converting it to a list of pairs. You can then go ahead and convert this list back into a dictionary which will have the required sorting order. – ThePretendProgrammer Nov 18 '17 at 14:08
  • I know there is a method that can sort them, but i need to use a min-heapsort to sort the values. – NOSMILE Nov 18 '17 at 14:11
  • 1
    the order of dictionary items can't be guaranteed even if items are added in certain order https://stackoverflow.com/questions/4007782/the-order-of-elements-in-dictionary – Slai Nov 18 '17 at 15:01

0 Answers0