nodes: 0, module; 1, function_definition; 2, function_name:_apply_sort; 3, parameters; 4, block; 5, identifier:cursor; 6, identifier:sort_by; 7, identifier:sort_direction; 8, expression_statement; 9, if_statement; 10, return_statement; 11, comment:"""
          Apply sort to a cursor.
  
          :param cursor: The cursor to apply sort on.
          :param sort_by: The field name to sort by.
          :param sort_direction: The direction to sort, "asc" or "desc".
          :return:
  
          """; 12, boolean_operator; 13, block; 14, else_clause; 15, call; 16, comparison_operator:sort_direction is not None; 17, comparison_operator:sort_direction.lower() == "desc"; 18, expression_statement; 19, block; 20, attribute; 21, argument_list; 22, identifier:sort_direction; 23, None; 24, call; 25, string:"desc"; 26, assignment; 27, expression_statement; 28, identifier:cursor; 29, identifier:sort; 30, identifier:sort_by; 31, identifier:sort; 32, attribute; 33, argument_list; 34, identifier:sort; 35, attribute; 36, assignment; 37, identifier:sort_direction; 38, identifier:lower; 39, identifier:pymongo; 40, identifier:DESCENDING; 41, identifier:sort; 42, attribute; 43, identifier:pymongo; 44, identifier:ASCENDING
code: def _apply_sort(cursor, sort_by, sort_direction):
          """
          Apply sort to a cursor.
  
          :param cursor: The cursor to apply sort on.
          :param sort_by: The field name to sort by.
          :param sort_direction: The direction to sort, "asc" or "desc".
          :return:
  
          """
          if sort_direction is not None and sort_direction.lower() == "desc":
              sort = pymongo.DESCENDING
          else:
              sort = pymongo.ASCENDING
          return cursor.sort(sort_by, sort)