nodes: 0, module; 1, function_definition; 2, function_name:sort_queryset; 3, parameters; 4, block; 5, identifier:queryset; 6, identifier:request; 7, default_parameter; 8, expression_statement; 9, expression_statement; 10, if_statement; 11, return_statement; 12, identifier:context; 13, None; 14, comment:""" Returns a sorted queryset
      The context argument is only used in the template tag
      """; 15, assignment; 16, identifier:sort_by; 17, block; 18, identifier:queryset; 19, identifier:sort_by; 20, call; 21, if_statement; 22, attribute; 23, argument_list; 24, comparison_operator:sort_by in [el.name for el in queryset.model._meta.fields]; 25, block; 26, else_clause; 27, attribute; 28, identifier:get; 29, string; 30, identifier:sort_by; 31, list_comprehension; 32, expression_statement; 33, block; 34, identifier:request; 35, identifier:GET; 36, string_content:sort_by; 37, attribute; 38, for_in_clause; 39, assignment; 40, if_statement; 41, identifier:el; 42, identifier:name; 43, identifier:el; 44, attribute; 45, identifier:queryset; 46, call; 47, comparison_operator:sort_by in request.session; 48, block; 49, comment:# added else to fix a bug when using changelist; 50, comment:# TODO: use less ifs and more standard sorting; 51, elif_clause; 52, attribute; 53, identifier:fields; 54, attribute; 55, argument_list; 56, identifier:sort_by; 57, attribute; 58, expression_statement; 59, try_statement; 60, comparison_operator:context is not None; 61, comment:# sorted ascending; 62, block; 63, attribute; 64, identifier:_meta; 65, identifier:queryset; 66, identifier:order_by; 67, identifier:sort_by; 68, identifier:request; 69, identifier:session; 70, assignment; 71, block; 72, except_clause; 73, identifier:context; 74, None; 75, if_statement; 76, expression_statement; 77, identifier:queryset; 78, identifier:model; 79, identifier:sort_by; 80, subscript; 81, expression_statement; 82, block; 83, comparison_operator:sort_by[0] != '-'; 84, block; 85, comment:# sorted descending; 86, else_clause; 87, assignment; 88, attribute; 89, identifier:sort_by; 90, assignment; 91, raise_statement; 92, subscript; 93, string; 94, expression_statement; 95, block; 96, identifier:queryset; 97, call; 98, identifier:request; 99, identifier:session; 100, identifier:queryset; 101, call; 102, identifier:sort_by; 103, integer:0; 104, string_content:-; 105, assignment; 106, expression_statement; 107, attribute; 108, argument_list; 109, attribute; 110, argument_list; 111, identifier:sort_by; 112, subscript; 113, assignment; 114, identifier:queryset; 115, identifier:order_by; 116, identifier:sort_by; 117, identifier:queryset; 118, identifier:order_by; 119, identifier:sort_by; 120, attribute; 121, binary_operator:int(sort_by) - 1; 122, identifier:sort_by; 123, binary_operator:'-' + context['cl'].list_display[abs(int(sort_by)) - 1]; 124, subscript; 125, identifier:list_display; 126, call; 127, integer:1; 128, string; 129, subscript; 130, identifier:context; 131, string; 132, identifier:int; 133, argument_list; 134, string_content:-; 135, attribute; 136, binary_operator:abs(int(sort_by)) - 1; 137, string_content:cl; 138, identifier:sort_by; 139, subscript; 140, identifier:list_display; 141, call; 142, integer:1; 143, identifier:context; 144, string; 145, identifier:abs; 146, argument_list; 147, string_content:cl; 148, call; 149, identifier:int; 150, argument_list; 151, identifier:sort_by
code: def sort_queryset(queryset, request, context=None):
      """ Returns a sorted queryset
      The context argument is only used in the template tag
      """
      sort_by = request.GET.get('sort_by')
      if sort_by:
          if sort_by in [el.name for el in queryset.model._meta.fields]:
              queryset = queryset.order_by(sort_by)
          else:
              if sort_by in request.session:
                  sort_by = request.session[sort_by]
                  try:
                      queryset = queryset.order_by(sort_by)
                  except:
                      raise
              # added else to fix a bug when using changelist
              # TODO: use less ifs and more standard sorting
              elif context is not None:
                  # sorted ascending
                  if sort_by[0] != '-':
                      sort_by = context['cl'].list_display[int(sort_by) - 1]
                  # sorted descending
                  else: 
                      sort_by = '-' + context['cl'].list_display[abs(int(sort_by)) - 1]
                  queryset = queryset.order_by(sort_by)
      return queryset