Controlling the sort order of the list from the admin user interface
I have a list of data that users can control the sort order.
It looks something like this.
Apples /\ \/
Oranges /\ \/
pears /\ \/
banana /\ \/
Pineapples /\ \/
etc /\ \/
/ \ and / are for up and down arrows
When the user clicks, I want to get the current sort order and clear it behind it (so it has a lower value and will be higher in the sort order.
When the user clicks down, I do the opposite
I guess I also need to establish if there is something else in the list with a new value that I want to assign and adjust the sort order of items in the opposite direction.
Does this seem like a good way to achieve this?
Or is there a smarter way?
Anyone got a sample code. I am using .net 3.5 so linq is ok
The list is kept in a cache in memory - IList<mycustomobj>
any grateful feedback
EDIT: The problems that I'm worried about is when there is no sort order at first - everything will be zero. And my proposed algorithm has an implicit assumption that the sort order will always be continuous. But what happens when a row is deleted? there will be a space in the list of sorting orders, so just adding 1 and removing 1 might not make any difference - how should I handle this scenario?
a source to share
This is the way to do it. In my application we use drag-and-drop or one up arrow and one down arrow to move the selected item up / down.
If you have a property that determines the sort order when the user clicks the up arrow, you just need to rearrange the current sort position of the element with the previous sort position of the element, unless it is the first element (do nothing in this case). Use reverse logic for the down arrow ...
Edit:
If your positions don't have a sort position yet, you can just go through your list and assign a sort position from 1 to n. For a deleted row, decrease the sort position of all the following items by one.
a source to share