def _sort_map_entries(category_map, sort_alpha):
 things = []
 for (title, entry) in category_map['entries'].items():
  if ((entry['sort_key'] is None) and sort_alpha):
   entry['sort_key'] = title
  things.append((title, entry, TYPE_ENTRY))
 for (title, category) in category_map['subcategories'].items():
  things.append((title, category, TYPE_SUBCATEGORY))
  _sort_map_entries(category_map['subcategories'][title], sort_alpha)
 category_map['children'] = [(x[0], x[2]) for x in sorted(things, key=(lambda x: x[1]['sort_key']))]

def _sort_map_entries ( category_map , sort_alpha ) : things = [ ] for ( title , entry ) in category_map [ 'entries' ] . items ( ) : if ( ( entry [ 'sort_key' ] is None ) and sort_alpha ) : entry [ 'sort_key' ] = title things . append ( ( title , entry , TYPE_ENTRY ) ) for ( title , category ) in category_map [ 'subcategories' ] . items ( ) : things . append ( ( title , category , TYPE_SUBCATEGORY ) ) _sort_map_entries ( category_map [ 'subcategories' ] [ title ] , sort_alpha ) category_map [ 'children' ] = [ ( x [ 0 ] , x [ 2 ] ) for x in sorted ( things , key = ( lambda x : x [ 1 ] [ 'sort_key' ] ) ) ]
internal helper method to list category entries according to the provided sort order .