title: Problems sorting data in django
body: <p>I have three database models, Story, StoryVote and Comment.</p>
  
  <pre><code>class Story(models.Model):
      user = models.ForeignKey(User)
      group = models.ForeignKey(Group, blank=True, null=True)
      date_added = models.DateTimeField(auto_now_add=True)
      date_modified = models.DateTimeField(auto_now=True)
      location = models.CharField(max_length=100)
      title = models.CharField(max_length=150)
      description = models.CharField(blank=True, null=True, max_length=2000)
      exp_text = models.TextField()
  
      def __unicode__(self):
          return self.title
  
  class Comment(models.Model):
      user = models.ForeignKey(User)
      date_added = models.DateTimeField(auto_now_add=True)
      date_modified = models.DateTimeField(auto_now=True)
      emailOnReply = models.NullBooleanField(blank=True, null=True)
      comment_text = models.TextField()
      story = models.ForeignKey(Story, related_name="comments")
  
      def __unicode__(self):
          return self.comment_text
  
  class StoryVote(models.Model):
      votes = models.IntegerField()
      story = models.ForeignKey(Story, related_name="votes")
  
      def __unicode__(self):
          return self.votes
  </code></pre>
  
  <p>I'm using:</p>
  
  <pre><code>all_stories = Story.objects.all()
  </code></pre>
  
  <p>to fetch details for all stories and im using this to fetch the comments in the template and count the number of comments that has been made to each story:</p>
  
  <pre><code>{% for story in all_stories %}
  {{ story.title }}
  {{ story.comments.all|length }}
  {% endfor %}
  </code></pre>
  
  <p>and im using the following to get the votes:</p>
  
  <pre><code>{% for story in all_stories %}
  {{ story.title }}
  {{ story.votes }}
  {% endfor %}
  </code></pre>
  
  <p>This works nicely but i want to get the output sorted.
  I have read about a number of ways to sort the data but nothing works.
  At first of course i was using the order_by() function but since im sorting on the "comments" field which is a related field to another model the order_by function work by sorting the data in table im in and it doesnt sort the data coming from the related table, so im getting all kinds of duplicates and no sorting.</p>
  
  <p>After that i thought i would use the distinct function but that doesnt work when you use a related field to sort on, so thats out, too. Then i tried to use the dictsort() function in the template itself, so i fetched the necessary data like this:</p>
  
  <pre><code>commented = list()
  i=0
  for story in most_commented:
  commented.append({'title': story.title, 'comments': story.comments.all().count()})
  i = i + 1
  </code></pre>
  
  <p>and sending the list with dicts to the template and calling the dictsort() but nothing happened when calling that function:</p>
  
  <pre><code>commented|dictsort:"title"
  </code></pre>
  
  <p>So im lost about how to get this data sorted. Any help is appriciated.</p>