return sorted query as string