Reordering list <T> using lambda

I need to reorder the list of elements so that the selected element moves to the end of the list, and the last element replaces the previous one, and the previous element replaces the previous one, etc.

For example, if I have a list of ten elements, and the selected element is in position 5, this element goes to position 9 and 9 replaces 8, and 8 replaces 7 and 7 replaces 6 and 6 takes position 5. I managed to get the desired result using this code:

List<int> numList = new List<int>(); int selectedNum = 5;//Selected at runtime for (int i = 0; i < 10; i++) numList.Add(i); int numListCount = numList.Count-1; int tempNum = numList[numListCount]; List<int> tempList = numList.GetRange(selectedNum + 1,(numList.Count-selectedNum) - 2); numList[numListCount] = selectedNum; numList.RemoveRange(selectedNum, (numList.Count-selectedNum)-1); numList.InsertRange(selectedNum, tempList); numList.Insert(numListCount - 1, tempNum); 

Result:

0,1,2,3,4,6,7,8,9,5

I'm sure my code is ugly and inefficient: I have two questions:

  • Is it possible to get the same result with Lambda? If not, then
  • How can I clarify my code. Thanks.
+6
source share
4 answers

You can use the Delete method to remove the selected item and Add method to add it to the end:

 List<int> numList = new List<int>(); for (int i = 0; i < 10; i++) numList.Add(2 * i); int selectedNum = 6; // selected at runtime numList.Remove(selectedNum); numList.Add(selectedNum); 

Before:

 0 2 4 6 8 10 12 14 16 18 

After removal (6):

 0 2 4 8 10 12 14 16 18 

After adding (6):

 0 2 4 8 10 12 14 16 18 6 

If you want to move the item at the selected index, you can use the RemoveAt method instead of the Delete method :

 List<int> numList = new List<int>(); for (int i = 0; i < 10; i++) numList.Add(2 * i); int selectedIndex = 5; // selected at runtime int selectedNum = numList[selectedIndex]; numList.RemoveAt(selectedIndex); numList.Add(selectedNum); 

Before:

 0 2 4 6 8 10 12 14 16 18 

After RemoveAt (5):

 0 2 4 6 8 12 14 16 18 

After adding (10):

 0 2 4 6 8 12 14 16 18 10 

Using LINQ, you must create a new list in which the selected item is deleted and added. An in-place upgrade, as shown above, is much more efficient.

 List<int> numList = new List<int>(); for (int i = 0; i < 10; i++) numList.Add(2 * i); int selectedIndex = 5; // selected at runtime List<int> newNumList = numList.Take(selectedIndex) .Concat(numList.Skip(selectedIndex + 1)) .Concat(numList.Skip(selectedIndex).Take(1)) .ToList(); 

numList:

 0 2 4 6 8 10 12 14 16 18 

newNumList:

 0 2 4 6 8 12 14 16 18 10 
+8
source

As far as I understand, you just want to move this element to the bottom of the list correctly?

 List<int> list = new List<int>(); for (int i = 0; i < 10; i++) numList.Add(i); int temp = list[5]; list.RemoveAt(5); list.Add(temp); 

EDIT: I understand that you knew the position of the item you wanted to move (5). If you know the meaning, then another answer posted the correct one for this

+1
source

You do not need all the extra stuff, including a list of topics. You can just do

 numList.Remove(selectedNum); numList.Add(selectedNum); 

Just like that.

+1
source

Can't check it now, but this should do the trick:

 var temp = list.Take(index-1).Concat(list.Skip(index)).Concat(list[index]); list = temp; 
0
source

Source: https://habr.com/ru/post/896900/


All Articles