score:7
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 );
Source: stackoverflow.com
Related Query
- Easiest Way to Reorder a sequence
- Favorite way to create an new IEnumerable<T> sequence from a single value?
- Easiest way to Rotate a List in c#
- Recommended way to check if a sequence is empty
- What is the easiest way to find the LINQ statement for a SQL statement
- Easiest way to convert list to a comma separated string by a Specific Property?
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- Easiest way to check record is exist or not in Linq to Entity Query
- Best way to handle redundant code that has repeated logic?
- What is the easiest way to save a LINQ query for later use?
- LINQ Source Code Available
- Easiest way to check numbers
- Using linq what is the easiest way to conv list<long> to list<int>?
- How does this linq code that splits a sequence work?
- Efficient way to unindent lines of code stored in a string
- Easiest way to get a common base class from a collection of types
- What is the easiest way to seeing if there are any matches across a few DateTime arrays?
- .NET 4 Code Contracts: "requires unproven: source != null"
- Is there a way to speed up this code that finds data changes in two XML files?
- Is there a way to determine whether IEnumerable<T> is a sequence generator or true collection?
- Query expressions over source type 'dynamic' or with a join sequence of type 'dynamic' are not allowed
- Easiest way to get "next" element in a sequence?
- Easiest way to implement .Contains() on a ICollection<Tuple<T1,T2>>
- creating Linq to sqlite dbml from DbLinq source code
- What would be a reasonably fast way to code this sql query in c#?
- Code Cleanup: Best way to split long statement over multiple lines
- Easiest way to return list<T> without first element
- Is there any way to make Code Contracts work with LINQ?
- Easiest way to populate empty list of Guids with random values
- C# Modular way to wrap a null-check around x code
More Query from same tag
- Linq query order by giving me issues
- C# Mongodb merge List<BsonDocument> into single BsonArray
- LINQ to XML contents of child records
- linq index out of bounds
- In LINQ-SQL, wrap the DataContext is an using statement - pros cons
- How to "query" List<T>
- How to take the Max value from a List of object where the same objects exists with many duplicate rows
- How can I read a CSV from Webserver using c# with LINQ? (ClickOnce deployment)
- optional include with entity framework
- Why GetFiles from a remote folder using Linq is hanging
- PlatformNotSupportedException when executing a general LINQ query
- Lambda expression on array property
- Linq to NHibernate repository implementation details
- Using LINQ to order a list using an existing array
- Splitting a deferred IEnumerable<T> into two sequences without re-evaluation?
- Select Single Value with ADO.Net Data Services and LINQ
- Searching any word using Linq
- How to get row count using LINQ on a file
- Updating records from a List in the database using LINQ
- Custom Query won't pass to view through viewmodel
- .NetCore C# - Can LINQ Query operators be seperated by logic?
- linq return single row from a goup
- Replace value in DataColumn
- Datacontext ExecuteCommand parameters in IN statement
- EntityFramework - Group by unordered pair in LINQ query
- Getting error while Parsing the XML data
- Foreach iteration,getting same values in a class
- Learning LINQ: QuickSort
- Linq where clause using filter object
- Avoiding the 2100 parameter limit in LINQ to SQL