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.

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