I have a list of pairs like
[[4,1],[1,2],[2,3]]
The idea is to sort them so that the second index of the first node matches the first of the second. In this example, the list is sorted. It is assumed that the list can always be unambiguously entered into this form. The list is never cyclical.
Now, if possible, I need a compare comparator, which can get this form:
x = [[4,1],[1,2],[2,3]] x.sort(compare)
Suppose that the compare function returns one of two values, corresponding to more and less. Whether this is possible, and if so, depends on the sorting algorithm.
If this is not possible, is it possible with two passes (possibly with different comparators) or with any fixed number of passes.
I wrote this in python, but my question is not specific.
source share