If you’re buying new I’d vote for the Nissan. The Sonata is getting better all the time, but the Altima has an established track record of good reliability.
If you’re shopping for a used car all bets are off. The only thing that matters with a used car is how it was maintained by its previous owner(s).
You might want to consult Consumer Reports Magazine for more informaion about the reliability of different makes and models.