Inventory sorting algorithm - UQdeco2800/2022-studio-2 GitHub Wiki

This wiki page will briefly discuss the implementation of the inventory sorting algorithm.

Code Logic

Inventory has a stacking system that attaches a quantity attribute to each item stored in the inventory. The collection that stores items using lists that are dynamically sized. The quantity on the other hand uses an array. This means if an item is removed from the list the quantity indices will not automatically shift forward and causes problems.

Therefore, in sprint 3 I implemented sorting for each removal of an item. It shifts every item's quantity after the removed Entity.

public void sortInventory(int index, List list, int[] quantity) {
    if (list.size() > index) {
        for (int i = index; i < list.size(); ) {
            quantity[i] = quantity[++i];
        }
    }
}

Back to Inventory Page

Author

  • Li-Sung Ou
  • GitHub: @PeterOu8
  • Discord: Secret Agent Randy Beans#6754
  • Slack: Li-Sung Ou
⚠️ **GitHub.com Fallback** ⚠️