Sonic can be a pain sometimes, but honestly, its manageable. If you get sonic off the edge, its game over for him unless he reads it and responds appropriately.
SDSH Dair off the side on his way up from down low, jump as soon as the first Dair ends and you can get another one, if he doesn't air dodge, he gets spiked, if he does air dodge the first Dair and you read which way he DI's the air dodge, your second dAir will hit. Nothing to be scared of when sonic is recovering from down low.
If he does a dash attack and you shield it, you can reverse extended dash grab out of the shield, his only escape is up or properly timed spot dodge.
If you grab sonic, it is a guaranteed KO after sonic reaches 35% before the grab combo starts. You can true combo dThrow>ShortDashFullHop Uair>SecondJump KneeOfJustice.
If close enough to the edge, you can dthrow>SDFH Uair>SJ Dair
This can net you the spike and is easier to true combo than the knee. Although, you need to be careful, at lower percents the spike might not be strong enough to kill sonic due to his great up B vertical distance.
Also be weary of sonic edge guarding, Sonic can Dair at the edge for the spike and still recover, try to time your air dodge properly and if you have to, jump up B early to try and go above him as he is dropping low where you normally go.
Falcon kick can be used to recover I'd you are at the upper corners blast boxes, it will finish recovering when you are just above the edge and you can still jump and up b after it finished, just make sure you are facing the right way.
Once sonic is at 35% you can even KO him with a Dthrow>SDFH Knee
Also, Sonics aerials will beat out pretty much anything you throw, a knee that sweet spots will trade, and if sonic is at 45%+ when this happens, the knee will kill as long as you are near the edge of the stage.
Jab works to stop it but don't rely on it.
You have to play this match up based on your opponent.
I recommend one extreme or the other, or to randomly switch between.
Start out extremely defensive, then once you get an opening go all offense for the rest of the stock. Don't let him breath, similar to how Cap handles little mac.
Careful that you don't always shield when he starts the spin dash, all he has to do is cancel it with shield and dash grab you. Try to connect fair so it will trade with his aerials, if you are not comfortable with that, then play more defensive, but don't roll often. Spot dodge when you can, otherwise shff uair and nair and wait until you can get that dash grab which = ton of damage
Good luck, also the others covered most of your options already, so I didn't need to repeat what they said