title: Sorting and indexing into a list in a Django template?
body: <p>How can you perform complex sorting on an object before passing it to the template? For example, here is my view:</p>
  
  <pre><code>@login_required
  def overview(request):
     physicians = PhysicianGroup.objects.get(pk=physician_group).physicians
  
  for physician in physicians.all():
      physician.service_patients.order_by('bed__room__unit', 'bed__room__order', 'bed__order')
  
  return render_to_response('hospitalists/overview.html', RequestContext(request,  {'physicians': physicians,}))
  </code></pre>
  
  <p>The physicians object is not ordered correctly in the template. Why not?</p>
  
  <p>Additionally, how do you index into a list inside the template? For example, (this doesn't work):</p>
  
  <pre><code>{% for note_type in note_types %}
     &lt;div&gt;&lt;h3&gt;{{ note_type }}&lt;/h3&gt;
     {% for notes in note_sets.index(parent.forloop.counter0) %}
     #only want to display the notes of this note_type!
        {% for note in notes %}
           &lt;p&gt;{{ note }}&lt;/p&gt;
        {% endfor %}
     {% endfor %}
     &lt;/div&gt;
  {% endfor %}
  </code></pre>
  
  <p>Thanks a bunch, Pete</p>