code: def sort_by_size(self, group_limit=None, discard_others=False,
                       others_label='others'):
          
          # sort groups by number of elements
          self.groups = OrderedDict(sorted(six.iteritems(self.groups),
                                           key=lambda x: len(x[1]),
                                           reverse=True))
  
          # if group-limit is provided, combine remaining groups
          if group_limit is not None:
  
              # now group together all groups that did not make the limit
              if not discard_others:
                  group_keys = self.groups.keys()[group_limit - 1:]
                  self.groups.setdefault(others_label, list())
              else:
                  group_keys = self.groups.keys()[group_limit:]
  
              # only go to second last (-1), since the 'others' group is now last
              for g in group_keys:
                  if not discard_others:
                      self.groups[others_label].extend(self.groups[g])
                  del self.groups[g]
  
              # remove if empty
              if (others_label in self.groups and
                      len(self.groups[others_label]) == 0):
                  del self.groups[others_label]
  
          # remove others group regardless of limit if requested
          if discard_others and others_label in self.groups:
              del self.groups[others_label]
docs: Sort the groups by the number of elements they contain, descending.
  
          Also has option to limit the number of groups. If this option is
          chosen, the remaining elements are placed into another group with the
          name specified with others_label. if discard_others is True, the others
          group is removed instead.