Here is a sheet that should work. You can sort by clicking the green triangle in Column E and sorting Z->A. Or once you copy this to your account, I created a script you can add that will auto sort after you add the cost value.
Did it work? Or does it need adjustment?