@contextfunction
def htsort(context, objects):
    "Sort objects based on request"
    if not objects or 'request' not in context:
        # Don't bother trying sorting if we can't do it
        return objects

    request = context['request']

    if 'sorting' not in request.GET or not hasattr(objects, 'order_by') or not hasattr(objects, 'model'):
        # Dont bother if there's nothing to sort on
        return objects

    args = request.GET.getlist('sorting')
    fields = objects.model._meta.get_all_field_names()
    for arg in args:
        field_name = arg.lstrip('-')
        if field_name in fields:
            field = objects.model._meta.get_field(field_name)
            if isinstance(field, models.ManyToManyField):
                agg_field = agg_arg = str(field_name % 'sorting_%s')
                if arg[0] == '-':
                    agg_arg = '-' + agg_arg
                kwargs = {agg_field: models.Count(field_name)}
                objects = objects.annotate(**kwargs).order_by(agg_arg)
            else:
                objects = objects.order_by(arg)

    return objects.distinct()
dataset/ETHPy150Open treeio/treeio/treeio/core/templatetags/modules.py/htsort/swapped operands of `%`