score:11
Create a list extension. Call as List<T>.Move(1, MoveDirection.Up)
.
public static class ListExtensions
{
public static void Move<T>(this IList<T> list, int iIndexToMove,
MoveDirection direction)
{
if (direction == MoveDirection.Up)
{
var old = list[iIndexToMove - 1];
list[iIndexToMove - 1] = list[iIndexToMove];
list[iIndexToMove] = old;
}
else
{
var old = list[iIndexToMove + 1];
list[iIndexToMove + 1] = list[iIndexToMove];
list[iIndexToMove] = old;
}
}
}
public enum MoveDirection
{
Up,
Down
}
Things to consider
Exception handling - what if you are trying to move the bottom element down or top element up? You will get an index out of range because you can't move these up or down.
You could improve this and prevent handling exception by extending functionality to moving the top element to the bottom element and bottom element down to the top element etc.
score:0
How about using SortedDictionary<int, Room>
instead of a list. You could store an index in as a Key of the Dictionary and just swap the values when needed.
score:0
Swapping places with the room that used to be above should do it:
int roomIndex = parentBuilding.Rooms.IndexOf(room);
if (roomIndex == 0)
{
return;
}
var wasAbove = parentBuilding.Rooms[roomIndex - 1];
parentBuilding.Rooms[roomIndex - 1] = room;
parentBuilding.Rooms[roomIndex] = wasAbove;
That said, I 'm not sure that this is the best object model for the situation; it's not clear that the order of rooms in the list plays a role, and it's also not clear how a room can be "moved up" -- what does that mean?
It might be better to have a RoomPlacement
class that aggregates a room and enough information to locate it, and work with that instead.
score:0
try this:
int newIndex = whateverIndexYouWantItAt;
int oldIndex = parentBuilding.Rooms.IndexOf(room);
var item = parentBuilding.Rooms[oldIndex];
list.RemoveAt(oldIndex);
if (newIndex > oldIndex) newIndex--;
parentBuilding.Rooms.Insert(newIndex, item);
score:4
Just do a swap:
int roomIndex = parentBuilding.Rooms.IndexOf(room);
if (roomIndex == 0)
{
return;
}
else
{
// move this room up.
var temp = parentBuilding.Rooms[index-1];
parentBuilding.Rooms[index-1] = parentBuilding.Rooms[index];
parentBuilding.Rooms[index] = temp;
}
score:1
Personally, I'd make extension method:
static void Swap<TSource>(this IList<TSource> source, int fromIndex, int toIndex)
{
if (source == null)
throw new ArgumentNullExcpetion("source");
TSource tmp = source[toIndex];
source[toIndex] = source[fromIndex];
source[fromIndex] = tmp;
}
Usage:
if (moveDirection == MoveDirection.UP)
{
int roomIndex = parentBuilding.Rooms.IndexOf(room);
if (roomIndex == 0)
{
return;
}
else
{
// move this room up.
parentBuilding.Rooms.Swap(roomIndex, roomIndex - 1);
}
}
Source: stackoverflow.com
Related Articles
- LINQ List<> Moving Elements up and down
- Construct a list of wpf Hyperlink elements from an XML source file using Linq
- c# Linq or code to extract groups from a single list of source data
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- How to get all elements except the n'th element in a List using Linq
- Using Linq query inside List<T>.AddRange to conditionally add elements from one list to another
- LINQ Source Code Available
- Compare two list elements with LINQ
- Get groups of 4 elements from name value list using LINQ in C#
- How do I use Linq to find the elements of a list that are not present in another list?
- Combine Elements in a List based on Type and Summate their Values, LINQ
- Apply a predicate on all elements of a list using LINQ
- C# Linq List Contains Similar Elements
- Create a List of elements from a DataTable LINQ Column
- Get the count of distinct elements from a list of lists using LINQ
- How can I use drop down list value (String) to filter linq results?
- LINQ in C#. Check if one list contains elements from another one
- LINQ query returns old results when source list is re-initialized
- Using LINQ how to transform a List of Lists using elements index
- C# - Linq optimize code with List and Where clause
- Ordering Linq list by array elements
- creating Linq to sqlite dbml from DbLinq source code
- Extract List elements of specific types with LINQ
- Check which elements are on one list comparing to another list LINQ
- LINQ for removing elements that are started with other element from list
- Linq for selecting elements from list 1 that exist on list 2 by comparsion between 2 properties values
- Linq to get List with most elements and biggest diff inside it's elements from a List<List<int>>
- Perform manipulation on all elements except last element in a List using Linq
- Grouping List elements and map to new model using Linq
- C# JSON.Net parse and get list of all elements matching a value using LINQ
- LINQ with two From clauses
- how to update the multiple rows at a time using linq to sql?
- BestPractice: Pros and Cons for using AutoMapper or LINQ (LINQ to Objects) for mapping between a Domain Model and a Presentation Model
- How do I use linq within a loop?
- Serialize linq query so that it can be executed somewhere else
- Incorrect number of arguments supplied for call to method 'Boolean Equals
- ASP.NET - Enumerate through SiteMapNode.ChildNodes
- LINQ compare string and DB column type 'text'
- How do I format date literals in dynamic linq?
- LINQ query to navigate an object hierachy
- LINQ Lambda Join Error - cannot be inferred from the usage
- Filtering first item in List<Contract> using LINQ
- What does "Replace with single call to single" mean?
- List of Users with list of Roles in Identity 2.2.1
- Update the list object
- C# LINQ To Sql Default Object Instantiation Over Optimization?
- Optimization on data retrieval
- LINQ suggestion C#
- linq vb.net change value
- how to run linq on XxmlElement rather than XElement in C#