Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by dARkStorN- · Apr 09, 2019 at 06:44 PM · arraylistsort

How do i move a list object at the end of the list while shifting back the others?

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 didnt 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[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:

alt text

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 doesnt 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.

listorder.jpg (29.1 kB)
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RobAnthem · Apr 09, 2019 at 06:51 PM 0
Share
 public List<T> $$anonymous$$oveToLast(List<T> myList, int index)
 {
     var temp = myList[index];
     int x = 0;
     for (int i = 0; i < myList.Count; i++)
     {
         if (i != index)
         {
             myList[x++] = myList[i];
         }
     }
     myList[myList.Length -1] = temp;
     return myList;
 }

Pretty sure this should work, just thought it for off the top of my head, but it looks solid.

As a note, you'll have to replace the Generic T with your class type, as you cannot return generic lists like that.

avatar image dARkStorN- RobAnthem · Apr 09, 2019 at 08:15 PM 0
Share

In your suggesting i dont achieve what i want because your loop looks like this:

alt text
At the end, i override both my first and last index where their previous values are lost.

forloop.jpg (34.7 kB)
avatar image RobAnthem dARkStorN- · Apr 09, 2019 at 09:08 PM 0
Share

Did you actually try it? I don't see the issue here, it moves all elements down except the one you are saving for last, then moves the intended element into the last position of the array. While still keeping the count and order. Also you are storing the myList[index] BEFORE the loop, so it doesn't matter what's in the index after the loop, then you assign the stored value to the last index...

In my example, by using X, you skip over the element you are moving but without increasing X if it's the element, therefore the next element AFTER the one you're moving, moves into the index of the one you are moving. So your example is 100% wrong, because myList[4] would = myList[5] and myList[5] would = myList[2]

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by azilvl · Apr 09, 2019 at 09:15 PM

this should do it.

 public static void Move<T>(this List<T> list, int oldIndex, int newIndex)
         {
             List<T> tempList = new List<T>(list);
             T item = list[oldIndex];
             tempList.RemoveAt(oldIndex);
             list.Clear();
             int j = 0;
             for (int i = 0; i < tempList.Count + 1; i++)
             {
                 list.Add(i == newIndex ? item : tempList[j]);
                 j += i == newIndex ? 0 : 1;
             }
         }


edit: turns out lists already have an Insert method so this can be achived with a lot less effort.

 public static void Move<T>(this List<T> list, int oldIndex, int newIndex)
         {
             T item = list[oldIndex];
             list.RemoveAt(oldIndex);
             list.Insert(newIndex, item);
         }

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RobAnthem · Apr 09, 2019 at 09:42 PM 0
Share

except that allocates ram for another list when it can be achieved with the existing list.

avatar image Owen-Reynolds · Apr 09, 2019 at 09:47 PM 0
Share

Not sure you need the tempList or the loop. Just list.Remove(oldIndex) then list.InsertAt(newIndex, item);. I think you have to subtract 1 if new is greater than old, to account for the indexes moving. Actually "C# move element in list" on StackExchange has the short way, with an explanation.

avatar image azilvl Owen-Reynolds · Apr 09, 2019 at 10:07 PM 0
Share

@Owen-Reynolds yeah didn't know there was a list.insert().

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

122 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Destroy GameObject after certain amount is reached? 1 Answer

Cant figure out how to implement a inventory system 1 Answer

Simple way to access data of different types within a nested array? 0 Answers

Organize data in a multi-column table 1 Answer

Trouble switching to 2nd object in list. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges