Question

I need a Sorted list, but after I remove an item from the list I then need to adjust the keys of the other items before adding new items to the list.

You are not allowed to change the key for the items in a "SortedList".

What tool would be best for doing this.

Example code

  timedEvQue.Add(3, "First");
  timedEvQue.Add(7, "Second");
  timedEvQue.Add(9, "Third");
  int decAmnt = (int)timedEvQue.Keys[0];
  timedEvQue.RemoveAt(0);

  for (int i = 0; i < timedEvQue.Count; ++i)
  {
    timedEvQue.Keys[i] = timedEvQue.Keys[i] - decAmnt; //runtime error here
  }

  timedEvQue.Add(5, "Forth");
Was it helpful?

Solution

There isn't typically a change key operation for dictionary/hash map type data structures as they would essentially just remove and add the item again. So just remove and add the item back.

timedEvQue.Add(3, "First");
timedEvQue.Add(7, "Second");
timedEvQue.Add(9, "Third");
int decAmnt = (int)timedEvQue.Keys[0];
timedEvQue.RemoveAt(0);

for (int i = 0; i < timedEvQue.Count; ++i)
{
    int oldKey = timedEvQue.Keys[i];
    string val = timedEvQue[oldKey];
    int newKey = oldKey - decAmnt;
    timedEvQue.Remove(oldKey);
    timedEvQue.Add(newKey, val);
}

timedEvQue.Add(5, "Forth");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top