Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change items position in List<string>

I have one list with 3 items

reg.ToList()    Count = 3   System.Collections.Generic.List<string>
[0] "Text0" string
[1] "Text1" string
[2] "Text2" string

I want to change all item positions, first postion instead last one, and remaining items to stand up with one position

for this example

[0] "Text1" string
[1] "Text2" string
[2] "Text0" string
like image 226
Alex Avatar asked Dec 01 '22 16:12

Alex


1 Answers

Use RemoveAt method right after getting ahold of your first item, then add it back.

var item = list[0];
list.RemoveAt(0);
list.Add(item);

RemoveAt will be more efficient than Remove because it won't try to search through your list and compare values needlessly.

like image 144
Crono Avatar answered Dec 10 '22 11:12

Crono