Do you want to rank them or just sort them? Sorting based on B and then A will, i think, give you what you want?