It's based on comparative medals, not comparative levels. Personally, I think it's a bad system. The highest possible advantage and medal awards are given to high level bottom feeders with maxed armies. I've got a pretty tough base, but I can't tell you how many times I've been 2 starred for 13 medals by level 150+ with lvl 30 generals, heavy tanks, blimps, etc. (The equivalent 5 star loss would be 33 medals.) Oh, I'm currently lvl 98 with about 2400 medals.
Ironically, I give up 13 medals for a 2 star loss to a lvl 163 attacker (65 levels above me), and I would get 1 medal for 5 starring the same person's base. Hmm… Well thought out system… (I suppose I could drop medals and attack lvl 33 bases…)