Generic List - moving an item within the list
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Moving an item inside a List<T> sounds trivial until index shifting produces off-by-one bugs. This operation appears in drag-and-drop UIs, ordered workflows, ranking systems, and settings screens. A reliable implementation needs to validate indexes, handle no-op moves, and account for the way removal changes later positions.
The Basic Remove-Then-Insert Pattern
The usual way to move an item is to remove it from its current position and insert it at the target position.
The forward-move adjustment is the part people miss. After removal, the list becomes shorter, so a destination index to the right shifts left by one.
See the Index Shift in Practice
Here is a small example:
This prints:
That result surprises people at first. The call means "move the item currently at index one into the position before what was originally index three after removal adjustment." If your API semantics should mean "place it at the final absolute index," you need to document that clearly and possibly expose a different helper.
Support Moving by Value Carefully
Sometimes callers know the item value, not the index. A wrapper can handle that case.
This is convenient, but it only moves the first matching item. If duplicates are possible, index-based calls are safer because they remove ambiguity.
Use an Immutable Variant When Mutation Is Risky
Some codebases prefer returning a new reordered list rather than mutating the original input.
This approach works well in state-driven architectures where shared mutable collections make debugging harder.
Clarify Index Semantics for UI Code
Many bugs come from disagreement about what toIndex means. In a drag-and-drop interface, the UI may report a drop location based on the original list, the partially updated list, or a slot between items. Your helper should define one interpretation and stick to it.
If the UI layer and the list helper use different semantics, the implementation may look correct in isolation while still producing wrong order in the final screen.
Performance Characteristics
For List<T>, both RemoveAt and Insert can shift many elements, so a single move is O(n). That is usually fine for normal UI lists. If you are reordering very large collections many times, measure first before reaching for a more complex data structure.
In many real applications, rendering, persistence, or network updates cost more than the in-memory list move itself.
Common Pitfalls
The most common mistake is forgetting the destination adjustment when moving an item forward in the list.
Another issue is failing to validate indexes, which turns a reorder bug into a runtime exception.
Value-based movement can also be dangerous in lists with duplicates because IndexOf only finds the first match.
Finally, teams often skip writing a shared helper and duplicate slightly different move logic in several places. That is how reorder bugs become hard to track down.
Summary
- Moving an item in
List<T>is usually implemented as remove then insert. - Forward moves require destination adjustment because removal shifts later indexes.
- Validate bounds and handle no-op moves explicitly.
- Prefer index-based APIs when duplicates make value-based movement ambiguous.
- Use an immutable wrapper when you want predictable state updates without in-place mutation.
Related reading
- Genetic algorithm - new generations getting worse
- Genetic algorithm and Tetris
- Genetic algorithm resource
- Get a filtered list of files in a directory
- Get a random element in single direction linked list by one time traverse
- Get a random item from a JavaScript array
- generic NOT constraint where T IEnumerable
- Generics in C, using type of a variable as parameter

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.