- Home /
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.
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.
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'.
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.
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--
}
}
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. :)
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.
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
Follow this Question
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