Swap two items in List

前端 未结 5 852
被撕碎了的回忆
被撕碎了的回忆 2020-11-30 05:20

Is there a LINQ way to swap the position of two items inside a list?

5条回答
  •  我在风中等你
    2020-11-30 06:05

    List has a Reverse() method, however it only reverses the order of two (or more) consecutive items.

    your_list.Reverse(index, 2);
    

    Where the second parameter 2 indicates we are reversing the order of 2 items, starting with the item at the given index.

    Source: https://msdn.microsoft.com/en-us/library/hf2ay11y(v=vs.110).aspx

提交回复
热议问题