title: How to sort dictionaries by keys in Python
body: <p>Can anyone tell me how I can sort this:</p>
  
  <pre><code>{'a': [1, 2, 3], 'c': ['one', 'two'], 'b': ['blah', 'bhasdf', 'asdf'], 'd': ['asdf', 'wer', 'asdf', 'zxcv']}
  </code></pre>
  
  <p>into</p>
  
  <pre><code>{'a': [1, 2, 3], 'b': ['blah', 'bhasdf', 'asdf'], 'c': ['one', 'two'],'d': ['asdf', 'wer', 'asdf', 'zxcv']}
  </code></pre>
  
  <p>?
  Thanks!</p>
  
  <p>UPDATE 1, code sample:</p>
  
  <p>So I am doing linguistics. One article is broken down to words that are stored in a database and have all kinds of properties including para ID and sentence ID. The task: trying to rebuild the original text.</p>
  
  <p>Get 500 consecutive words from DB</p>
  
  <pre><code>words = Words.objects.all()[wordId:wordId+500]
  # I first create paragraphs, through which I can loop later in my django template,
  # and in each para will be a list of words (also dictionaries). 
  # So i am trying to get a dictionary with values that are lists of dictionaries. 
  # 'pp' i make just for shorthanding a long-named variable.
  paras={}
  para_high = para_low =  words[0].belongs_to_paragraph
  for w in words:
      last_word = w
      pp = w.belongs_to_paragraph
      if pp &gt;para_high:
          para_high = pp
      if pp &lt; para_low:
          para_low = pp
      if pp in paras:
          paras[pp].append(w)
      else:
          list = [w]
          paras[pp] = list
  # Since there are blank lines between paragraphs, in rebuilding the text as it 
      #  looked originally, I need to insert blank lines. 
      # Since i have the ID's of the paragraphs and they go somewhat like that: 1,3,4,8,9 
      #(the gaps between 1 &amp; 3 and 4 &amp; 8 i have to fill in with something else, 
      # which is why i had para_low and para_high to loop the range. 
  isbr = True
  for i in range(para_low, para_high+1):
      if i in paras:
          isbr = True
      else:
          if isbr:
              paras[i]=['break']
              isbr = False
          else:
              paras[i]=[]
  </code></pre>
  
  <p>At this point, however, if I try to loop the dict and rebuild the text, some later id'd paragraphs come before previous ones, and that just doesn't do it.</p>
  
  <p>UPDATE 2, loop code:</p>
  
  <pre><code>        {% for k,v in wording.iteritems()  %}
          {% if v[0] == 'break' %}
          &lt;br/&gt;
          {% else %}
          &lt;/div&gt;&lt;div class="p"&gt;{% for word in v %}{% if word.special==0%} {% endif %}&lt;span class="word {% if word.special == 0%}clickable{% endif%}" wid="{{word.id}}" special="{{word.special}}" somethingElse={{word.somethingElse}}&gt;{{ word.word }}&lt;/span&gt;{% endfor %}
          {% endif %}
      {% endfor %}
  </code></pre>