This method would work, but it may be quite ineffective, depending on your usage of special triggers fireing on "unit requires an item".
You can order a unit to put a certain item into a specified slot, that works exactly the same way as if you were doing it manually.
Sorting
You start from the item type you want to be put in slot 1 and move that item to slot 1, if it is not already there.
Afterwards you pick the itemtype that is supposed to be put in slot 2 and do the equivalent actions.
You go on using that method until you reached slot 6.
Troubleshoot
This method will be working properly as long as a hero carries only
one item of each type. This means that you will have to check first whether he is using 2 items of the same type and drop the item that violated this rule. If the hero did not have another item of that type, you may start with that sorting.
This system can be improved to assure that there are empty slots when not having an item that fits into the specified types, but this should work well enough
