Could you just keep the items in a list and imply the Order
property from the physcial order of the items in the list?
Given an Item
class without an order property, like so:
class Item
{
public readonly string Value;
public Item(string value)
{
Value = value;
}
public override string ToString()
{
return Value;
}
}
You could write a simple collection class for Items
which has a Move()
method to let you move an item from one index to another:
class Items: IEnumerable<Item>
{
private readonly List<Item> _items = new List<Item>();
public void Add(Item item)
{
_items.Add(item);
}
public int Count
{
get { return _items.Count; }
}
public void Move(int oldIndex, int newIndex)
{
Item item = _items[oldIndex];
_items.RemoveAt(oldIndex);
_items.Insert(newIndex, item);
}
IEnumerator<Item> IEnumerable<Item>.GetEnumerator()
{
return _items.GetEnumerator();
}
public IEnumerator GetEnumerator()
{
return _items.GetEnumerator();
}
}
You can use Items
like so:
var items = new Items
{
new Item("a0"),
new Item("a1"),
new Item("a2"),
new Item("a3")
};
// ...
items.Move(0, 2); // Move the item at index 0 to index 2.
Then when you need the Order
you can synthesize it from the list's physical order like so:
var orderedItems = items.Select((item, index) => new { Item = item, Order = index});