May I suggest a book: Only don't know by Zen Master Seung Sah. It is not directly related to your problem, but I think its title provides an answer to your problem.
The way I deal with these sort of judgmental thoughts, is by "not knowing". 99% of these judgements are based on our superficial assumptions about these people's lives, based on a few signs we can observe. If you think about it that way, it becomes clear that we construct an idea of these people in our heads, and then we judge that idea! We think we are superior or inferior than an idea we ourselves constructed. Isn't that funny?
In Buddhism, one of the key elements of applying Dharma in practice is becoming good friends with ambiguity. Normally we are used to clarity and the confidence that comes from clarity but in Buddhism we recognize that this clarity is illusory. In fact we know very little about the world, may be 5%, and the rest we infer ourselves. From this inference, which most of the time is not very accurate and sometimes outright wrong, come all kinds of problems. So one of the essential skills in Buddhism is remaining open, unconclusive, ambiguous with regards to people and the world. This is known as "beginner's mind", and is what's referred by Zen Master Seung Sah as "only don't know".
Thanissaro Bhikkhu at some point gave a very good talk (I listened to it in recording, it must be available online but I can't find it at the moment) on Ego and Egolessness. The gist of his message was that, basically, Ego is a giant overgeneralization that we make, a horrific simplification about ourselves. We create this caricature of who we are, and then struggle to fit that with (again) a caricature of the world that we likewise created. Instead, the Buddhist approach is to be free from generalizations, free from the caricatures. 99% of our judgemental thoughts come from these sort of generalizations or caricatures - of either ourselves or others.
So the main technique to overcome these thoughts of contempt and social comparison, whether in your favor or not, is to learn to challenge your own assumptions and generalizations. Perhaps talk to some people and try to really understand their personal world. It may not be the happiest world but in my experience it is almost always a world that has its own logic, its own sense of goodness, honesty, pride. It will usually have some samsaric elements to be sure: attachments, Ego, side-taking, karmic tangles - but within that setting there is always a sentient being that is doing their best to be good. The definition of "good" may be skewed in their system of coordinates, due to traumas and all kinds of karmic situations, and their level of skill and motivation may vary - but they always want something good and try to achieve that within their limits.
When you stop creating caricatures of people, and try to see the world through their eyes - nothing but compassion arises.