1

First of all i must say i have spent all my day searching my answer before i posted this question here. The closest answer i get is here but some of the answers didn't work for me while others are too complicated for me to understand. Let me explain clearly what i want to achieve: For simplicity lets think i have a list that consists of "6" objects. I randomly select an index in the list, lets say, myList[index=2]. Then i want this object to go at the end of the list meaning its index value is now 5. Also i want to re-arrange the untouched objects to get the same size of list without empty index value. After all these steps it should like this:

ListOrder

At the moment my code is this:

 public static void Move<T>(this List<T> list, int oldIndex, int newIndex)
 {
     if ((oldIndex == newIndex) || (0 > oldIndex) || (oldIndex >= list.Count) || (0 > newIndex) ||
         (newIndex >= list.Count)) return;
     // local variables
     var i = 0;
     T tmp = list[oldIndex];
     for (i = oldIndex+1; i < newIndex; i++)
     {
         list[i] = list[i - 1];
     }        
     list[newIndex] = tmp;
 }  

However, as you can predict it doesn't work. Debug.DrawLine to gameobjects in this list shows me when i move the items through the list with my code leave me two objects one myList[0] and one myList[5] which i added. The other lines disappears after.

slava
  • 791
  • 1
  • 11
  • 26
  • Sounds `LinkedList` could help you...for example, you have 1 2 3 4 5 and then take 3, so the next order should be 1 2 4 5 3? – Johnny Apr 09 '19 at 20:28
  • I think you want `+ 1` instead of `- 1`, and `oldIndex` instead of `oldIndex+1`, and `i < newIndex - 1` instead of `i < newIndex`. But this only works when newIndex > oldIndex. if it's the other way around than the code you have would be more accurate. – BlueMonkMN Apr 09 '19 at 20:33
  • 1
    Just to clarify a little further, you're looking to create a method that extracts an object at a given index, and then adds it at the end of your list? Or extracts an object at a given index, and then adds it at another specified index? – SharpNip Apr 09 '19 at 20:35
  • @SharpNip it's the former. When i call this method, i always give newIndex (list.Count-1) so it goes at the end of my list. – Ahmet Bugra Apr 09 '19 at 20:39
  • Well, you already save a copy from "oldIndex" into variable "tmp", so I think you just want to "list.RemoveAt(oldIndex)" followed by "list.Add(tmp)" to add it at the end. Maybe I am missing something... – BernieP Apr 09 '19 at 20:41
  • @Johnny if your numbers represent my objects then yes. I want to take the third object and make it last on my list so its index is now (list.count -1 = 4) while the object 1 has index 0, object 2 has index 1, object 4 has index 2, object 5 has index 3. – Ahmet Bugra Apr 09 '19 at 20:42

1 Answers1

4

You're overthinking this.

var value = list[index];
list.RemoveAt(index);
list.Add(value);
VPellen
  • 1,071
  • 5
  • 10
  • I'd just add a `if (oldIndex >= list.Count) return;` right before. – SharpNip Apr 09 '19 at 20:46
  • @SharpNip an index within the list will never be `>= list.Count` .. I guess you ment `if(index == list.Count - 1) return;`. For the other cases where an incorrect index is given I would always rather throw an error/exception in order to debug properly why even a wrong index was provided than hiding this fact by simply returning ;) – derHugo Apr 10 '19 at 05:18
  • I am sorry for the duplicate post. I tried this solution before but i couldnt make it work. Must have missed something. Thanks all for the answers. – Ahmet Bugra Apr 10 '19 at 08:42
  • @derHugo, the index that's passed is not from "within the list", it's passed as a parameter to the method, which is why it is not desired to have any execution if the index passed is greater than the count of the items in the list. Also, `if(index == list.Count - 1)` will throw an exception if the index passed to the method is the last object in the List or greater, and considering the original code, that is not what is desired. – SharpNip Apr 10 '19 at 13:13
  • @SharpNip I think you didn't quite get my point. 1. You can return on `if(index == list.Count - 1)` because then it already **is** the last item in the list so there is nothing to do. 2. The answer provided here doesn't even require that `oldindex` parameter since it needs only the one current `index` of the element that is than moved to the end. 3. As I explained I would not simply return in case of an invalid input but rather want an exception/error thrown in order to properly find out what I did wrong instead of obfuscating that by simply returning without any feedback. – derHugo Apr 10 '19 at 14:29
  • @derHugo Sorry. I was wrong, you were right. – SharpNip Apr 10 '19 at 14:36