title: Ordering dictionary in context Django
body: <p>I looked at this <a href="https://stackoverflow.com/questions/5250276/how-to-render-an-ordered-dictionary-in-django-templateshttp://">thread</a>, with an example of sorting dictionaries.</p>
  
  <p>I have a dictionary of programme objects where the key is a programme object and the value is a lookup of the number of related Project objects.</p>
  
  <pre><code>def DepartmentDetail(request, pk):
      department = Department.objects.get(pk=pk)
      programmes = Programme.objects.all().filter(department=department).exclude(active=False).order_by('long_name')
      combi = {}
      for p in programmes:
          prj = Project.objects.all().filter(programme=p)
          combi[p] = str(len(prj))
      return render(request, 'sysadmin/department.html',{'department': department, 'programmes': programmes, 'combi': sorted(combi.items())})
  </code></pre>
  
  <p>In the model, Programme returns a string 'long_name', so I believe that I am trying to sort a string key and a string value. </p>
  
  <p>In the template I get to the keys and values as so,</p>
  
  <pre><code>{% for programme, n in combi %}
  </code></pre>
  
  <p>This gives me the error.. </p>
  
  <pre><code>unorderable types: Programme() &lt; Programme()
  </code></pre>
  
  <p>I don't really understand the error, in the <a href="https://docs.python.org/3/howto/sorting.html" rel="nofollow noreferrer">python 3 documentation</a> it states that the sorted() method accepts any iterable - So why does this happen?</p>
  
  <p>I'm looking at collections.OrderedDict to solve the problem, but I want to know why this doesn't work.</p>
  
  <p>Thanx.</p>