I have a list with objects that have a weight-property. I'm trying to rearrange the objects in the list, so that the weights are distributed evenly over the 2 halves (halves being left & right. So for example, if the list is 4 items long, indexes 0 and 1 are the left half and indexes 2 and 3 are the left half).
I'm trying to jump from (list of 4 items) index 0 -> 3 -> 1 -> 2, or maybe even 0 -> 3 -> 2 -> 1, which ever distributes the weights more evenly.
I tried messing around but haven't been able to come up with something working. (It should also work for lists with an odd number of items.)
bool left = true;
List<Column> orderedColumns = _columns.OrderByDescending(x => x._totalWeight).ToList();
for (int i = 0; i < _columns.Count; i++)
{
Column columnToPlace = orderedColumns[0];
int index = i;
if (!left)
{
index = _columns.Count - i;
}
if (left && i > 0)
{
index--;
}
_columns[index] = columnToPlace;
orderedColumns.RemoveAt(0);
left = !left;
}