It seems like it was already almost doing what you wanted. Your edit made it do almost the opposite.
It was selecting the most damaged tool, not the tool with the least durability.
You will want to replace
`candidateStack.getItemDamage() > replacementStack.getItemDamage()`
`candidateStack.getMaxDamage() - candidateStack.getItemDamage() < replacementStack.getMaxDamage() - replacementStack.getItemDamage()`
Thanks for the answer. This did work for armor! But it did not work for tools on the toolbar, which leaves me dumbfounded. Any further ideas?