I figured it out.
NFITC1: Item1 starts at 319 and item2 starts at 318
The above is incorrect. item1 always starts from 319 while item2 is the next inventory index to search.
E.G;
item1 = 319 to 0, item2 = 0
item1 = 319 to 0, item2 = 1
Once item1's inventory index is < than item2's, end the sorting function for that index.
var length = player.inventory.items.Length - 1;
/// Start at first inventory index (item2)
for (var i = 0; i <= length; i++)
{
/// Cycle through inventory from end to start (item1), searching for an item that can be swapped with item2.
for (var j = length ; j >= 0; j--)
{
var i1 = player.inventory.items[j];
var i2 = player.inventory.items[i];
var swapID = ShouldSwap(i1, i2);
/// As long as the search index is greater than the index of that which is being replaced,
/// run condition to see if a swap is required
if(j > i)
{
if (swapID == 1)
{
player.inventory.items[i] = i1;
player.inventory.items[j] = i2;
}
}
/// If the search index becomes less than the index of which is being replaced, end the sorting of this index
else
{ break; }
}
}
So essentially, starting from the top index, search for an item to replace it with by searching all inventory index's starting from the bottom. If the search index gets < than that of what it is trying to replace, end the sort for that index, then start again from the next top index.
