If it is a minor sin, it does not compromise moral uprightness; if it is a major sin, the two views apply.
However, it may be said here: If someone exercising kingship and authority performs the good deeds commanded and refrains from the evil deeds prohibited to such an extent that his reward exceeds the punishment for the obligations he neglects or the prohibitions he violates, then his good deeds outweigh his bad deeds. If someone else falls short in the acts of obedience that this ruler performs, while remaining free of his sins, there are “three possible cases”:
The surplus of the ruler’s good deeds may be greater or less than the total good deeds of the other person. If that surplus is greater, the ruler is superior; if it is less, he is inferior; and if the two are equal, they are equal in merit. This is what justice requires and what the texts of the Quran and Sunnah concerning reward and punishment entail. It rests upon the view that good and bad deeds are weighed against and offset one another in determining recompense, and likewise in assessing moral uprightness.
As for those who say that a person deserves the threatened punishment for a single major sin, even if he has many great good deeds, this reasoning does not apply according to their view. A group of scholars holds this position concerning moral uprightness. The first view, however, is more correct, as indicated by the texts.
From this arises a “question”: What if someone can perform a good deed of outweighing merit only by committing a sin whose punishment is less than the reward for that good deed? This has two cases: