def _get_sorted_methods(self, methods):
    if not methods:
      return methods
    def _sorted_methods_comparison(method_info1, method_info2):
      def _score_path(path):
        score = 0
        parts = path.split('/')
        for part in parts:
          score <<= 1
          if not part or part[0] != '{':
            score += 1
        score <<= 31 - len(parts)
        return score
      path_score1 = _score_path(method_info1[1].get('path', ''))
      path_score2 = _score_path(method_info2[1].get('path', ''))
      if path_score1 != path_score2:
        return path_score2 - path_score1
      path_result = cmp(method_info1[1].get('path', ''),
                        method_info2[1].get('path', ''))
      if path_result != 0:
        return path_result
      method_result = cmp(method_info1[1].get('httpMethod', ''),
                          method_info2[1].get('httpMethod', ''))
      return method_result
    return sorted(methods.items(), _sorted_methods_comparison)