Add advanced query function; sort