improve search and sort function