lst = [1,3,5,9,8,4,6,7,2] def sort(iterable,key=lambda a,b:a<b,reverse=False): ret = [] for x in iterable: for i,y in enumerate(ret): flag = key(x,y) if reverse else key(y,x) if flag: ret.insert(i,x) break else: ret.append(x) return ret print(sort(lst))