Sorted pattern matching