code: def ordering_url(self, field_name):
          
          path = self.request.path
          direction = ""
          query_params = self.request.GET.copy()
          ordering = self.request.GET.get("order", "").split(",")
          field = self._get_ordering_field_lookup(field_name)
          if not ordering:
              ordering = self.get_default_ordering()
          merged_ordering = list(ordering)  # copy the list
  
          for ordering_field in self.get_ordering_fields_lookups():
              if (ordering_field.lstrip("-") not in ordering) and (
                  ("-" + ordering_field.lstrip("-")) not in ordering
              ):
                  merged_ordering.append(ordering_field)
  
          new_ordering = []
          for item in merged_ordering:
              if item.lstrip("-") == field.lstrip("-"):
                  if (item[0] == "-") or not (item in ordering):
                      if item in ordering:
                          direction = "desc"
                      new_ordering.insert(0, item.lstrip("-"))
                  else:
                      direction = "asc"
                      new_ordering.insert(0, "-" + item)
  
          query_params["order"] = ",".join(new_ordering)
  
          return (path + "?" + query_params.urlencode(safe=","), direction)
docs: Creates a url link for sorting the given field.
  
          The direction of sorting will be either ascending, if the field is not
          yet sorted, or the opposite of the current sorting if sorted.