def csort(objs, key):
 idxs = dict(((obj, i) for (i, obj) in enumerate(objs)))
 return sorted(objs, key=(lambda obj: (key(obj), idxs[obj])))

def csort ( objs , key ) : idxs = dict ( ( ( obj , i ) for ( i , obj ) in enumerate ( objs ) ) ) return sorted ( objs , key = ( lambda obj : ( key ( obj ) , idxs [ obj ] ) ) )
order-preserving sorting function .