<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def legacy_filter_formatter(kwargs, attr_map):
 """Builds a filter for update and device apis :param kwargs: expected to contain {'filter/filters': {filter dict}} :returns: {'filter': 'url-encoded-validated-filter-string'} """

    params = _depluralise_filters_key(copy.copy(kwargs))
    new_filter = _get_filter(sdk_filter=params.pop('filter', {}), attr_map=attr_map)
    if new_filter:
        new_filter = sorted([(k.rsplit('__eq')[0], v) for k, v in new_filter.items()])
        params['filter'] = urllib.parse.urlencode(new_filter)
    return params