Fix the logic behind the selecting of alternative classes by attribute,
which used to prioritise later classes with exact matches over more
matches