Allow to write graphs in sorted order

 - with python3, graph output is no longer deterministic because of hash
   randomization
 - add sort argument to writer functions to make the output optionally
   predictable
 - fixes issue #1181