Python sort sprite dict into new dict sorted by name
<p>I have a dict which is part of a <code>SpriteSheet</code> class of the attribute <code>sprite_info</code>. <code>sprite info</code> holds the names and location and xd/yd of each sprite on the sheet, i.e. </p>

<pre><code>{'stone_Wall' : { 'x': '781', 'xd': '70', 'y': '568', 'yd': '70'} ... }
</code></pre>

<p>What I'd like to do is sort the dict by each name. In other words, there are other names in the list list <code>stone_Right</code> and <code>stone_Mid</code> and so on. The one thing they all have in common is <code>stone_</code>.</p>

<p>What's the most efficient way of doing this? My limited experience tells me to just go into a bunch of nested for-loops, but I know there's a better way.</p>

<p>Further clarification:</p>

<p>Once everything is sorted, I would like to separate the dict by name. Using my example, for any key that includes <code>stone</code> or <code>stone_</code>, add it to a new dict within the already existing dict.</p>

<p>You can't sort a <code>dict</code>, as it uses its own sorted tree structure internally to keep efficient.</p>

<p>You can use <code>OrderedDict</code> which will keep track of the order elements are added.</p>

<p>To created a sorted <code>OrderedDict</code> from a <code>dict</code>, sort the key/value pairs from the <code>dict</code> based on the <code>key</code>.</p>

<p>edit: having thought a little more, <code>sorted</code> will compare tuples element by element, so item zero (the key) will be compared first, and will also be unique (as <code>dict</code> has unique keys), so we don't need to do anything clever with the <code>sorted</code> <code>key</code> parameter.</p>

<pre><code>from collections import OrderedDict
# from operator import itemgetter

sprites = dict()
# sorted_sprites = OrderedDict(sorted(sprites.items(), key=itemgetter(0)))
sorted_sprites = OrderedDict(sorted(sprites.items()))  # equivalent to above
</code></pre>

<p><a href="https://docs.python.org/2/library/functions.html#sorted" rel="nofollow"><strong><code>sorted</code></strong></a> is a built-in function which returns a <code>list</code> from a sequence.</p>

<p>The <code>key</code> parameter determines how to order the values in the sequence. As we are passing <code>sprites.items()</code> it is getting tuple pairs e.g. <code>('stone_Wall', { 'x': '781', 'xd': '70', 'y': '568', 'yd': '70'})</code>, so the key we want is the zero-th element of the tuple, <code>'stone_Wall'</code>.</p>

<p><a href="https://docs.python.org/2/library/operator.html#operator.itemgetter" rel="nofollow"><strong><code>itemgetter</code></strong></a> is a functor which will retrieve a particular object (or objects) from a sequence. Here we ask it to get the zero-th.</p>

<p>However, as noted above, default tuple comparison will do this for us. See this related question: <a href="https://stackoverflow.com/questions/5292303/python-tuple-comparison">python tuple comparison</a></p>

python python-2.7 sorting dictionary pygame