I don't understand your sort algorithm, so I can't tell you how to implement it. But there is a general technique, which is to use the key
parameter in Python's builtin sort
function. In other words, you want to come up with some transformation of your data which Python would sort in the correct order, and then write that transformation as a Python function foo
and call sort(data, key=foo)
.
Example: if you had a list of strings "-"
, say ["1-1","1-2","3-1"]
and you wanted to sort by the second number and then the first, notice that Python would sort the data correctly if it were in the form [(1,1), (2,1), (1,3)]
i.e. a list of reversed tuples. So you would write a function
def key(s):
l, r = s.split("-")
return int(r), int(l)
and then sort the list with sort(l, key=key)
.