I have the following list:
List<string> l=new List<string()>{"a","b","c","d","1","2","3","4","a1","b2","c3","d4"};
And I have oldIndex
,size
and newIndex
.
Let's say for the above list oldIndex = 4
,size = 4
,newIndex = 0
.
The output list I would like is:
{"1","2","3","4","a","b","c","d","a1","b2","c3","d4"};
The solution I've found is(which is not working for all cases):
for(int i=0;i<size;i++)
{
if(oldIndex<newIndex)
{
var x=l[oldIndex];
l.RemoveAt(oldIndex);
l.Insert(newIndex+size-1,x);
}
else
{
var x=l[oldIndex+i];
l.RemoveAt(oldIndex+i);
l.Insert(newIndex+i,x);
}
}
Is there any better way to this, perhaps using LINQ or something better, or is the above the only way to this?
I found the above answer from this link. The difference in this question is that I would like to move a block, and not just single element.