title: custom JSON sort_keys order in Python
body: <p>Is there any way in Python 2.6 to supply a custom key or cmp function to JSON's sort_keys?</p>
  
  <p>I've got a list of dicts coming from JSON like so:</p>
  
  <pre><code>[
    {
      "key": "numberpuzzles1",
      "url": "number-puzzle-i.html",
      "title": "Number Puzzle I",
      "category": "nestedloops",
      "points": "60",
      "n": "087"
    },
    {
       "key": "gettingindividualdigits",
       "url": "getting-individual-digits.html",
       "title": "Getting Individual Digits",
       "category": "nestedloops",
       "points": "80",
       "n": "088"
    }
  ]
  </code></pre>
  
  <p>...which I've stored into the list variable <code>assigndb</code>.  I'd like to be able to load in the JSON, modify it, and serialized it back out with <code>dumps</code> (or whatever), keeping the <em>orders of the keys intact</em>.</p>
  
  <p>So far, I've tried something like this:</p>
  
  <pre><code>ordering = {'key': 0, 'url': 1, 'title': 2, 'category': 3,
               'flags': 4, 'points': 5, 'n': 6}
  
  def key_func(k):
      return ordering[k]
  
  # renumber assignments sequentially
  for (i, a) in enumerate(assigndb):
      a["n"] = "%03d" % (i+1)
  
  s = json.dumps(assigndb, indent=2, sort_keys=True, key=key_func)
  </code></pre>
  
  <p>...but of course <code>dumps</code> doesn't support a custom key like <code>list.sort()</code> does.  Something with a custom <code>JSONEncoder</code> maybe?  I can't seem to get it going.</p>