from typing import OrderedDict


def groupby_unsorted(a, key=lambda x: x, sort=False, get=lambda x: x):
    """
    group the elements of the input-array and return it as a dict with a list
    of the found values. optionally use a key- and a get- function.

    if sort is True, a OrderedDict with sorted keys will be returned

    roughly equivalent to:

        >>> # if only the input-array a is provided
        ... {unique value of a: [found copies of the unique value]}
        ... # if a and a key-function is provided
        ... {key(a) : [...values with the same key(a)...]}
        ... # if both a key- and a get-function is provided
        ... {key(a) : [get(x) for x in ...values with the same key(a)...]}

    """
    # always use an OrderedDict to ensure sort-order for python < 3.6
    d = OrderedDict()
    for item in a:
        d.setdefault(key(item), []).append(get(item))
    if sort is True:
        return OrderedDict(sorted(d.items()))
    else:
        return d