After a battle, in what order are EVs distributed in?
So, say I have a 252 HP / 252 SpD/ 3 Spe Magikarp. I fight 4 Trevenants (2 Attack EVs) and a Sudowoodo (2 Defense EVs) while holding a Power Lens (4 SpA EVs).
Which stat will gain EVs?

Well it depends on what Pokemon was beaten first, if you defeated Trevenant first, you get the Attack evs. If you defeated Sudowoodo first, You get the Defense EVs. The Power Lens activates after levelling up, which happens after you defeat a Pokemon.