I have a table with items that are displayed by their SortOrder. The SortOrders are not incremented very well, with values skipping anywhere from 1-100. What I want to do is flip-flop the SortOrder values of two items that are closest together, without necessarily knowing how close together they are. For example:
ItemX.SortOrder = 5;
ItemY.SortOrder = 26;
Assume no items have a sort order between 5-26.
My code needs to switch this to:
ItemX.SortOrder = 26;
ItemY.SortOrder = 5;
For some reason, my code is switching SortOrders with random items, such that:
ItemX.SortOrder = 5;
ItemY.SortOrder = 26;
ItemZ.SortOrder = 34;
Becomes:
ItemX.SortOrder = 34;
ItemY.SortOrder = 26;
ItemZ.SortOrder = 5;
I think its because the results of my query aren’t ordered the way I am picturing them. Here is my query code. If anything else is needed, just let me know.
itemToSwitch = DataSource.Items.Where(item => item.SortOrder > currentItem.SortOrder).First();
int? next = itemToSwitch.SortOrder;
int? previous = currentItem.SortOrder;
currentItem.SortOrder = next;
itemToSwitch.SortOrder = previous;
Sorry if my question is difficult to read or jumbled. I’m trying to be as clear as possible.
I’m suspecting you actually want:
If you’re reusing it, a
.ToList()might help