sorting improved, avoid searcing when pattern is blank