added sorting to patterns. fixed search