It's a really strange logic in sort method
Potential issues with code are:
You need to clone product collection before try to iterate it
category_id will be the same for all products because it's a ...
Oh man, again question from you about the same topic and without any answers
This code should work and I think here is everything is OK
I just can suggest to use correlation table for price field for ...