lst = [1,3,2,5,6,4] 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))