I have a class Item and a dictionary of Items. Each item in the dictionary has a unique priority (1 to N). When I remove an item from dictionary, all other priorities are updated. I want to implement some increase/decrease priority in the dictionary. If I want to increase the priority of a single item I swap priorities with the next lower item. The problem is increasing priorities of a collection of items
public class Item
{
public string key;
public string data;
public int Priority;
}
Dictionary<string, Item> allItems = new Dictionary<string, Item>();
public void AddToQueue(Item item)
{
item.Priority = allItems.Count + 1;
allItems[item.key] = item;
}
public void PriorityUp(Item it)
{
if(it.Priority <= 1)
return;
it.Priority--;
foreach(var item in allItems )
if(item.Value.Priority == it.Priority)
{
item.Value.Priority++;
break;
}
}
public void PriorityUp(IEnumerable<Item> items)
{
//TODO
}
I have dictionary in order to find efficiently an item. Increasing priority of some items must create some change in priorities of others
To be more clear: I have a collection of N items (list, array, dictionary...) I choosed dictionary because I must do some other operations also. Each item has a field Priority with some unique value 1<=P<=N.
I want to find the resulted Priority (1 to N) of all items when I select some and increase/decrease P.