A Cohein takes precedence to a Levite; a Levite, to an Israelite, [viz. (Deuteronomy 10:8): "At that time the L rd set apart the tribe of Levi, etc."]; an Israelite to a mamzer, [the first being "pedigreed," the second not]; a mamzer to a Nathin, [the first coming from an untainted drop; the second, from a tainted one]; a Nathin to a proselyte, [the first having been raised with us in holiness; the second not]; a proselyte to a freed slave, [the first not having been subsumed in "accursed," the second having been subsumed thus]. When is this so? When they are all equal (in eminence); but, as between a mamzer who is a Torah scholar and a high-priest who is an ignoramus, a mamzer who is a Torah scholar takes precedence to a high-priest who is an ignoramus, [as it is written (Proverbs 3:15): "It [Torah] is more precious than peninim (precious gems") — even (more precious) than the (the high-priest), who enters lifnai velifnim, (acronymic of "peninim"), "the innermost sanctum."]
Mishneh Torah, Gifts to the Poor
If we have before us many poor people or many captives, and there is not enough in the fund to sustain them, or to clothe them, or to redeem them all, a priest takes precedence over a Levite, a Levite over a [regular] Jew, a Jew over a chalel [an illegitimate child of a priest], a chalel over a shetuki [an illegitimate child with an unknown father], a shetuki over a asufi [a foundling], an asufi over a mamzer [a child who was conceived in a union forbidden by the Torah], a mamzer over a natin [a descendent of the Gibeonites], and a natin over a stranger, so long as the natin was praised with us in holiness, and a convert takes precedence over a freed slave, for he [the slave] was once one of the cursed.161See Mishnah Horayot 3:8.
To what does this refer? When both who are imprisoned are equal in wisdom. But if there was a High Priest who was an ignoramus and a mamzer who was a wise disciple, the wise disciple takes precedence. Anyone who is great in wisdom takes precedence over another. But if one of them [the captives] was one's rabbi or father, even if there is someone who is greater in wisdom, one's rabbi or father [takes precedence]. Even if there is someone there [among the captives] who is greater in wisdom than his rabbi or his father, so long as he [his father or rabbi] is a wise disciple, he [the father or the rabbi] takes precedence over the one who is greater than them in wisdom.162See Mishnah Horayot 3:8 and Babylonian Talmud Horayot 13a.