functions for ranking and ordering, I need some improvements to make the code simplier and to increase readability