title: Sorting A List In Descending Order Jinja
body: <p>I'm creating a website in Flask using the Jinja template engine. I have a list of announcements but no good way to sort them.</p>
  
  <p>They are in a table being called on by id right now. I want to sort it by newest first (which would mean the highest id) </p>
  
  <p>Like:</p>
  
  <ul>
  <li>P1
  <ul>
  <li>5</li>
  <li>4</li>
  <li>3</li>
  </ul></li>
  <li>P2<br>
  <ul>
  <li>2</li>
  <li>1</li>
  </ul></li>
  </ul>
  
  <p>In my HTML I have put this instruction for Jinja:</p>
  
  <pre><code>&lt;div id="main"&gt; 
  
  {% autoescape false %}
  {% for announcement in announcements.items|sort(attribute='id', reverse = True) %}
  
    &lt;a name={{announcement.title}}&gt;&lt;/a&gt;
    &lt;h1&gt;{{announcement.title}} - Posted on: {{announcement.date}}&lt;/h1&gt;
    {{announcement.body}}
    &lt;br/&gt;
    &lt;hr/&gt;
  
  {% endfor %}
  {% endautoescape %}
  
   &lt;p class = "footer"&gt;{% if announcements.has_prev %}&lt;a href="{{ url_for('index', page = announcements.prev_num) }}"&gt;&lt;&lt; Newer posts&lt;/a&gt;{% else %}&lt;&lt; Newer posts{% endif %} | 
   {% if announcements.has_next %}&lt;a href="{{ url_for('index', page = announcements.next_num) }}"&gt;Older posts &gt;&gt;&lt;/a&gt;{% else %}Older posts &gt;&gt;{% endif %} &lt;/p&gt;
  
  &lt;br/&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>And the python:</p>
  
  <pre><code>from flask import render_template, Markup
  from app import app
  from config import POSTS_PER_PAGE
  from models import Announcement, VideoAnnouncement
  
  @app.route('/')
  @app.route('/index')
  @app.route('/index/&lt;int:page&gt;')
  def index(page = 1):
      loggedOut = True
      announcements = Announcement.query.paginate(page, POSTS_PER_PAGE, True)
      videoAnnounce = VideoAnnouncement.query.all()
      return render_template("index.html", announcements = announcements, videoAnnouncements = videoAnnounce , loggedOut = loggedOut)
  </code></pre>
  
  <p>But that sorts like this:</p>
  
  <ul>
  <li>P1
  <ul>
  <li>3</li>
  <li>2</li>
  <li>1</li>
  </ul></li>
  <li>P2<br>
  <ul>
  <li>5</li>
  <li>4</li>
  </ul></li>
  </ul>
  
  <p>Is there anyway to sort it in descending order that won't get messed up by the pages?</p>
  
  <p>(I hope my question made sense)</p>