score:7

Accepted answer

the order property should not be an integer but a decimal. you can easily change the value and then sort by it to retrieve the items in any arbitrary order. the new value is the midpoint between the preceding and following values.

p.s. then you only have to change the order property for the item(s) in the list you want to reposition.

score:0

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});

score:0

i wrote the algorithm for you, that uses sorting and linkedlist collection:

using system;
using system.collections.generic;

namespace orderexample {

    public class a {
        public int order { get; set; } 
    }

    public class program {

        // change order so that a is ordered between b and c.
        public static void setorder(list<a> list, a a, a b, a c) {
            list.sort((x, y) => x.order.compareto(y.order));
            var linkedlist = new linkedlist<a>(list);
            var bnode = linkedlist.find(b);
            if (bnode != null) {
                linkedlist.remove(a);
                linkedlist.addafter(bnode, a);
                var i = 0;
                foreach (var value in linkedlist) {
                    value.order = i++;
                }                
            }
        }

        static void main() {
            var a0 = new a {order = 0};
            var a1 = new a {order = 1};
            var a2 = new a {order = 2};
            var a3 = new a {order = 3};
            var list = new list<a> {a0, a1, a2, a3};

            setorder(list, a0, a2, a3);
            foreach (var a in list) {
                console.out.writeline(a.order);
            }
            console.readkey();
        }
    }
}

score:1

if you can afford to i would number them with gaps in between:

a0.order is 10
a1.order is 20
a2.order is 30
a3.order is 40

this way you can simple reorder by picking a number within a gab.

a0.order is 35
a1.order is 20
a2.order is 30
a3.order is 40

after some iterations you may have no gabs left at some insertion point. with 100 items you can simple reset all the order numbers to have equal gabs again.

score:3

we have to differentiate between moving an element upwards (to the end) or downwards (to the beginning of the list). let's define two order numbers i and k where i < k.

move element i upwards to k:
the orders below i and above k are not affected.
the orders i+1 ... k are decreased by one and i becomes k.

a moving = list.where(a => a.order == i);
foreach (a x in list.where(a => a.order > i && a.order <= k)
{
    x.order--;
}
moving.order = k;

move element k downwards to i:
the orders below i and above k are not affected.
the orders i ... k-1 are increased by one and k becomes i.

a moving = list.where(a => a.order == k);
foreach (a x in list.where(a => a.order >= i && a.order < k)
{
    x.order++;
}
moving.order = i;

score:7

you could do something like this:

void moveandupdateorder(list<a> list, a item, int positiontoinsert)
{
    // order elements
    var ordered_list = list.orderby(a => a.order).tolist();

    // remove and insert at the proper position
    ordered_list.remove(item);
    ordered_list.insert(positiontoinsert, item);

    // update the order properties according to it's current index
    for ( int i = 0; i < ordered_list.count; i++ )
        ordered_list[i].order = i;
}

and then call it like this:

var a0 = new a { order = 0 };
var a1 = new a { order = 1 };
var a2 = new a { order = 2 };
var a3 = new a { order = 3 };

var list = new list<a>
{
    a0, a1, a2, a3
};

moveandupdateorder( list, a0, 2 );

Related Query

More Query from same tag