Given an Array and an Example-Array to sort to, write a function that sorts the Array following the Example-Array.

Assume Example Array catalogs all elements possibly seen in the input Array. However, the input Array does not necessarily have to have all elements seen in the Example.

Example:

Arr:
[1,3,4,4,4,4,5]

Example Arr:
[4,1,2,3,5]

Result:
[4,4,4,4,1,3,5]

Source: codewars | Difficulty: EASY
Tags: Fundamentals

Sorting

Data structures

Starter code: def example_sort(arr, example_arr):
	

Solutions:
def example_sort(arr, example_arr):
	return sorted(arr, key=example_arr.index)


def example_sort(arr, example_arr):
	keys = {k: i for (i, k) in enumerate(example_arr)}
	return sorted(arr, key=lambda a: keys[a])


def example_sort(a, b):
	return sorted(a, key=b.index)


from collections import Counter

def example_sort(arr, example_arr):
	c = Counter(arr)
	return [n for n in example_arr for _ in range(c[n])]


def example_sort(arr, example_arr):
	empty = []
	for i in example_arr:
		if i in arr:
			empty.extend([i] * arr.count(i))
	return empty


example_sort = lambda x, y: sorted(x, key=y.index)


def example_sort(arr, example_arr):
	d = {x: i for (i, x) in enumerate(example_arr)}
	return sorted(arr, key=d.__getitem__)


def example_sort(arr, example_arr):
	return [x for x in example_arr for y in arr if x == y]


def example_sort(arr, example_arr):
	sorted_arr = []
	number_count = {}
	for n in arr:
		number_count[n] = number_count.get(n, 0) + 1
	for n in example_arr:
		num_count = number_count.get(n, 0)
		for i in range(num_count):
			sorted_arr.append(n)
	return sorted_arr


def example_sort(arr, example_arr):
	return [a for e in example_arr for a in arr if a == e]