The Shariah doesn’t allow animals to be troubled without any pressing need. As far as sterilization of animals is concerned, the Jurists have clarified that sterilization of animals to gain any benefit or to remove any hurt is permissible and there is no problem in it. As mentioned in Fatawa A’alamgiri, Vol. 5, Pg. No. 357:
Translation: Apart from horse, there is no problem in sterilizing other animals provided it leads to some benefit. There is no problem in sterilization of a cat provided the sterilization leads to some benefit or removes some hurt.
The above clarifications make it clear that if sterilization is for benefit or to drive away some hurt, then it is permissible. If dogs are attacking passersby, then doing so is necessary to make sure that the men, women and children can go about their work without any harm.