Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Johnny H. · Jun 27, 2013 at 07:35 PM · c#arraynullsort

Reorder array with nulls at the end

Okay, I have a GameObject Array as an inventory (C#), and I want to reorder the items inside the array to put the null values of the array at the end, for example:

 inventory[0] = null;
 inventory[1] = "Sword";
 inventory[2] = "Armor";
 inventory[3] = null;
 inventory[4] = "Key";
 inventory[5] = null;

and I want to reorder to:

 inventory[0] = "Sword";
 inventory[1] = "Armor";
 inventory[2] = "Key";
 inventory[3] = null;
 inventory[4] = null;
 inventory[5] = null;

The array's lenght is always 6, and I don't need it to be sorted alphabetically, I only need the nulls at the end, how do I do it? Thanks.

Comment
Add comment · Show 1
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 Johnny H. · Jun 27, 2013 at 07:21 PM 0
Share

I actually managed to do it by myself, but since I'm not a programmer, I'm pretty sure this is a horrible approach, this is the code I came up with:

 public static void SortInventory()
 {
     GameObject[] tmp;
     tmp = new GameObject[slots];
     
     for(int i = 0; i < slots; i++)
     {
         if(inventory[i] != null)
         {
             for(int j = 0; j < slots; j++)
             {
                 if(tmp[j] == null)
                 {
                     tmp[j] = inventory[i];
                     inventory[i] = null;
                     break;
                 }
             }
         }
     }
     
     for(int k = 0; k < slots; k++)
     {
         if(tmp[k] != null)
         {
             for(int l = 0; l < slots; l++)
             {
                 if(inventory[l] == null)
                 {
                     inventory[l] = tmp[k];
                     tmp[k] = null;
                     break;
                 }
             }
         }
     }
 }

In this code I use a temporary array to reorder the values and then I switch back to the actual inventory array.

I would really appreciate a better solution to this problem, thanks.

1 Reply

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

Answer by Julien-Lynge · Jun 27, 2013 at 07:37 PM

Your solution looks like a fine start. How about keeping two indices: one starting in the last slot of the array ('last'), and one starting in the first ('first'). If you find a value, stick it in the new array at 'first' and increment 'first'. If you find a null, stick it in the new array at 'last' and decrement 'last'.

Comment
Add comment · Show 5 · 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 Johnny H. · Jun 27, 2013 at 07:55 PM 0
Share

I'm not sure if I understand exactly what you meant, as I said, I'm not a programmer, lol. But this would be just an improvement on this code, would it make a big difference regarding performance and such? I don't know, I though (think) there's a better and simpler way to do that.

avatar image Julien-Lynge · Jun 27, 2013 at 08:01 PM 1
Share

All of this really depends on the size of your array - if you have less than, say, 100 items, the difference isn't going to be noticeable. As you pass 1000 or 10000 items, you'll definitely want to care more about the speed of the algorithm.

Currently, this is what you have:

 for()
   for()
 for()
   for()

Nested for() loops are going to add up calculations quickly with a large array. What I'm proposing is:

 for()

so orders of magnitude faster for any sizeable array. Here's some pseudo-code to explain it:

 start = 0;
 end = array.Length - 1;

 for (int i = 0; i < array.Length; i++)
 {
   if (null)
   {
     put it in the new array at start
     start++
   }
   else
   {
     put it in the new array at end
     end--
   }
 }
avatar image Johnny H. · Jun 27, 2013 at 08:25 PM 0
Share

I got it working, here is the code:

 for (int i = 0; i < inventory.Length; i++)
 {
     if (inventory[i] == null)
     {
         tmp[start] = inventory[i];
         start++;
     }
     else
     {
         tmp[end] = inventory[i];
         end--;
     }
 }
 
 for (int i = 0; i < inventory.Length; i++)
 {
     if (tmp[i] == null)
     {
         inventory[start] = tmp[i];
         start++;
     }
     else
     {
         inventory[end] = tmp[i];
         end--;
     }
 }

Again, one loop to reorder the values with a temp array, and another to move the values back to the original array. I didn't run much tests on it, but it's working as far as I can tell, is this exactly what you meant?

I still would like to know if there are better aproaches to this problem, but if none comes up, I'm sure going to mark your answer as correct, very good, thanks. :)

avatar image Julien-Lynge · Jun 27, 2013 at 08:28 PM 0
Share

Getting close :)

It looks like you have your if statements flipped - if null, presumably you want it at the end.

You actually don't need the if/else in the second for() loop - once you go through the first, it's all ordered, so you can just copy it straight back to the original array. You actually don't necessarily need to copy it back to the original array - you can just throw away the original array and use the new one if you don't have any references anywhere else to the old array.

avatar image Johnny H. · Jun 27, 2013 at 08:41 PM 0
Share

Okay, this is the final version then (I think, lol)...

 for (int i = 0; i < inventory.Length; i++)
 {
     if (inventory[i] != null)
     {
         tmp[start] = inventory[i];
         start++;
     }
     else
     {
         tmp[end] = inventory[i];
         end--;
     }
 }
 
 for (int i = 0; i < inventory.Length; i++)
 {
     inventory[i] = tmp[i];
 }

Thanks for the help. :)

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

16 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

Related Questions

Parse.com and Unity integration 1 Answer

[C#] Sorting a List of Gameobjects Alphabetically 2 Answers

Check if Array.GetValue(i) is null 1 Answer

Sorting an array of GameObjects by their position 1 Answer

Multiple Cars not working 1 Answer


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