def filter_matches(match_text, candidates, case_sensitive, sort_key=(lambda x: x)):
 if case_sensitive:
  case_transform = _identity
 else:
  case_transform = _lower
 if match_text:
  match_text = case_transform(match_text)
  matches = [r for r in candidates if (match_text in case_transform(r))]
 else:
  matches = list(candidates)
 matches.sort(key=(lambda x: sort_key(case_transform(x))))
 return matches

def filter_matches ( match_text , candidates , case_sensitive , sort_key = ( lambda x : x ) ) : if case_sensitive : case_transform = _identity else : case_transform = _lower if match_text : match_text = case_transform ( match_text ) matches = [ r for r in candidates if ( match_text in case_transform ( r ) ) ] else : matches = list ( candidates ) matches . sort ( key = ( lambda x : sort_key ( case_transform ( x ) ) ) ) return matches
filter candidates and return the matches .