code: def sort_flavor_list(request, flavors, with_menu_label=True):
      def get_key(flavor, sort_key):
          try:
              return getattr(flavor, sort_key)
          except AttributeError:
              LOG.warning('Could not find sort key "%s". Using the default '
                          '"ram" instead.', sort_key)
              return getattr(flavor, 'ram')
      try:
          flavor_sort = getattr(settings, 'CREATE_INSTANCE_FLAVOR_SORT', {})
          sort_key = flavor_sort.get('key', 'ram')
          rev = flavor_sort.get('reverse', False)
          if not callable(sort_key):
              def key(flavor):
                  return get_key(flavor, sort_key)
          else:
              key = sort_key
          if with_menu_label:
              flavor_list = [(flavor.id, '%s' % flavor.name)
                             for flavor in sorted(flavors, key=key, reverse=rev)]
          else:
              flavor_list = sorted(flavors, key=key, reverse=rev)
          return flavor_list
      except Exception:
          exceptions.handle(request,
                            _('Unable to sort instance flavors.'))
          return []