I've done a few different Liquid only sorting posts here before so let's run over another. This one will cover sorting products based on the stock count of product (and variants within).
A demo is setup on the experiments store, on a specific collection.
The code is very similar to an earlier one put together for sorting by metafield value. For the quick version I'll include the Liquid file before. For more detailed info jump to the github repo.
grabbing the code...