add topological sort

Signed-off-by: Mattias Andrée <maandree@operamail.com>