Define a method that accepts 2 strings as parameters. The method returns the first string sorted by the second.

```python
sort_string("foos", "of")       == "oofs"
sort_string("string", "gnirts") == "gnirts"
sort_string("banana", "abn")    == "aaabnn"
```

To elaborate, the second string defines the ordering. It is possible that in the second string characters repeat, so you should remove repeating characters, leaving only the first occurrence.

Any character in the first string that does not appear in the second string should be sorted to the end of the result in original order.

Source: codewars | Difficulty: EASY
Tags: String algorithms

Sorting

Starter code: def sort_string(s, ordering):
	

Solutions:
def sort_string(s, ordering):
	answer = ''
	for o in ordering:
		answer += o * s.count(o)
		s = s.replace(o, '')
	return answer + s


def sort_string(s, ordering):
	dct = {c: -i for (i, c) in enumerate(reversed(ordering))}
	return ''.join(sorted(s, key=lambda c: dct.get(c, 1)))


sort_string = lambda s, o: ''.join(sorted(s, key=lambda x, d={c: i for (i, c) in reversed(list(enumerate(o)))}: d.get(x, len(o))))


def sort_string(st, order):
	return ''.join(sorted(list(st), key=lambda e: list(order).index(e) if e in order else len(order)))


def sort_string(stg, ordering):
	return ''.join(sorted(stg, key=lambda c: ordering.index(c) if c in ordering else float('inf')))


def sort_string(s, ordering):
	order = {}
	for (i, c) in enumerate(ordering):
		if c in order:
			continue
		order[c] = i
	return ''.join(sorted(s, key=lambda c: order.get(c, 9999)))


def sort_string(s, ordering):
	result = ''
	for i in ordering:
		if i in s and i not in result:
			result += i * s.count(i)
	return result + ''.join([c for c in s if c not in ordering])


def sort_string(s, ordering):
	result = sorted([c for c in s if c in ordering], key=ordering.find)
	result.extend([c for c in s if c not in ordering])
	return ''.join(result)


def sort_string(a, b):
	li = list(filter(lambda x: x in b, a))
	return ''.join(sorted(li, key=b.index) + [i for i in a if i not in li])


def sort_string(s, ordering):
	return ''.join(sorted([i for i in s if i in ordering], key=lambda x: ordering.index(x))) + ''.join(sorted([j for j in s if j not in ordering], key=lambda x: s.index(s)))