Make it work for Top-N and Bottom-N