This addOn automatically stacks and sorts your items.
Video demonstration (The minimap button is from an older version)
/cleanupbags (starts button placement mode)
/cleanupbank (starts button placement mode)
Use the mouse wheel to iterate through the visible frames to choose a parent for the button. When holding down ctrl the mouse wheel will resize the button instead. Finally click to place the button.
The highest priority is to fill special bags (e.g., herb bag) with fitting items.
Besides that, the primary sort order is:
hearthstone
mounts
special items (items of arbitrary categories that tend to be kept for a long time for some reason. e.g., cosmetic items like dartol's rod, items that give you some ability like cenarion beacon)
key items (keys that aren't actual keys. e.g., mara scepter, zf hammer, ubrs key)
tools
other soulbound items
reagents
consumables
quest items
high quality items (which aren't in any other category)
enchanting materials
herbs
common quality items (which aren't in any other category)
junk
soul shards
conjured items
The basic intuition for the primary sort order is how long items are expected to be kept around. The more "permanent" an item is the lower it is placed in your bags.
Within the primary groups items are further sorted by itemclass, itemequiploc, itemsubclass, itemname and stacksize/charges in this order of priority.