I have lists of tasks that I want to sort pseudo-topologically. Say, I have something like this:
enum Task {
case a(Int, Int)
case b(String)
case c(Bool)
}
I require all a
tasks to be executed before any b
tasks, and all b
tasks before all c
tasks. In addition, all tasks of the same time need to remain in the same order as in the input.
In computer science lingo, what I need is
- a stable sorting algorithm and
- a way to feed it a partial order.
Is there such a thing in the Swift standard library?
Observations:
Comparable
, a natural protocol to look at for sorting, requires the order to be total.sorted
can be made to work with partial orders, but is not stable.
As an alternative, a topological sorting algorithm would work as well, even though it would do more than I need.