added a sorting feature for queries