code: def validate_sort_fields(sort_fields):
      descending = set()
      def sort_order_filter(name):
          "Helper to remove flag and memoize sort order"
          if name.startswith('-'):
              name = name[1:]
              descending.add(name)
          return name
      sort_fields = validate_field_list(sort_fields, name_filter=sort_order_filter)
      log.debug("Sorting order is: %s" % ", ".join([('-' if i in descending else '') + i
          for i in sort_fields]))
      if not descending:
          return operator.attrgetter(*tuple(sort_fields))
      class Key(object):
          "Complex sort order key"
          def __init__(self, obj, *args):
              "Remember object to be compared"
              self.obj = obj
          def __lt__(self, other):
              "Compare to other key"
              for field in sort_fields:
                  lhs, rhs = getattr(self.obj, field), getattr(other.obj, field)
                  if lhs == rhs:
                      continue
                  return rhs < lhs if field in descending else lhs < rhs
              return False
      return Key