Select *, SortOrder(color1,color2,color3,color4) AS sortorder ; From mytable ; ORDER BY sortorder ... FUNCTION SortOrder(tcColor1,tcColor2,tcColor3,tcColor4) RETURN ; IIF(tcColor1="blue" or tcColor2="blue" or tcColor3 ="blue" or tcColor4=="blue", "1", "2") + ; IIF(tcColor1="green" or tcColor2="green" or tcColor3 ="green" or tcColor4=="green", "1", "2") + ; IIF(tcColor1="yellow" or tcColor2="yellow" or tcColor3 ="yellow" or tcColor4=="yellow", "1", "2") + ; IIF(tcColor1="white" or tcColor2="white" or tcColor3 ="white" or tcColor4=="white", "1", "2") + ; IIF(tcColor1="black" or tcColor2="black" or tcColor3 ="black" or tcColor4=="black", "1", "2") + ; IIF(tcColor1="other" or tcColor2="other" or tcColor3 ="other" or tcColor4=="other", "1", "2")Instead of using UDF you can include those IIF's directly in the query.