Help with Inventory System

I am attempting to make a game similar to Terraria, where you can collect resources and have them go into your inventory. The problem I am having is using a UI to represent the inventory. If you collected a rock and had nothing else in you inventory, then it should go to the top left part of the inventory, but if a piece of dirt is already there it should go to the right of it. My current idea for solving this would to use a variable saying how many item types there are, and multiply that by the size of and inventory slot to decide where a new item should go. Is there a better way of implementing this, because my way seems tedious and inefficient.

In b106 there is support for Arrays, so I would plan to use that. Then you can have an array of structures, where each structure is a lot in your inventory with an item name an count.
You can start now with a static structure of structures where the parent structure just uses 0, 1, 2, etc. as the keys.
As far as placement algorithms go, whenever the player picks up a new item type then just scan through the inventory in order looking for an item that matches that type and if found then increment the count, if you reach the end of the list then add a new slot with a count of 1 for that type.
If you want to autosort by count you can do that immeadiately afterwards, or have a button that the player has to press to sort (which will possible provide some performance relief)