'Find, group and sort the options
@param field: the Field
@param value: the currently selected value(s)'

def _options(self, field, value):

    options = self.options
    if (options is None):
        requires = field.requires
        if (not isinstance(requires, (list, tuple))):
            requires = [requires]
        if hasattr(requires[0], 'options'):
            options = requires[0].options()
        else:
            options = []
    elif isinstance(options, dict):
        options = options.items()
    none = self.none
    exclude = (('',) if (none is not None) else ('', None))
    options = [((s3_unicode(k) if (k is not None) else none), (v.flatten() if hasattr(v, 'flatten') else s3_unicode(v))) for (k, v) in options if (k not in exclude)]
    if (not options):
        return {'empty': current.T('no    options    available')}
    if (not isinstance(value, (list, tuple))):
        values = [value]
    else:
        values = value
    values = [s3_unicode(v) for v in values]
    helptext = {}
    help_field = self.help_field
    if help_field:
        if callable(help_field):
            help_field = help_field(options)
        if isinstance(help_field, dict):
            for key in help_field.keys():
                helptext[s3_unicode(key)] = help_field[key]
        else:
            (ktablename, pkey, multiple) = s3_get_foreign_key(field)
            if (ktablename is not None):
                ktable = current.s3db[ktablename]
                if hasattr(ktable, help_field):
                    keys = [k for (k, v) in options if k.isdigit()]
                    query = ktable[pkey].belongs(keys)
                    rows = current.db(query).select(ktable[pkey], ktable[help_field])
                    for row in rows:
                        helptext[unicode(row[pkey])] = row[help_field]
    letter_options = {}
    for (key, label) in options:
        letter = label
        if letter:
            letter = s3_unicode(label).upper()[0]
            if (letter in letter_options):
                letter_options[letter].append((key, label))
            else:
                letter_options[letter] = [(key, label)]
    all_letters = letter_options.keys()
    import locale
    if all_letters:
        all_letters.sort(locale.strcoll)
        first_letter = min(u'A', all_letters[0])
        last_letter = max(u'Z', all_letters[(-1)])
    else:
        size = 0
    size = self.size
    cols = self.cols
    close_group = self._close_group
    if (size and (len(options) > size) and (len(letter_options) > 1)):
        groups = []
        group = {'letters': [first_letter], 'items': []}
        for letter in all_letters:
            group_items = group['items']
            current_size = len(group_items)
            items = letter_options[letter]
            if (current_size and ((current_size + len(items)) > size)):
                close_group(group, values, helptext)
                groups.append(group)
                group = {'letters': [letter], 'items': items}
            else:
                if (letter != group['letters'][(-1)]):
                    group['letters'].append(letter)
                group['items'].extend(items)
        if len(group['items']):
            if (group['letters'][(-1)] != last_letter):
                group['letters'].append(last_letter)
            close_group(group, values, helptext)
            groups.append(group)
    else:
        group = {'letters': None, 'items': options}
        close_group(group, values, helptext, sort=self.sort)
        groups = [group]
    return {'groups': groups}
