From 6a06f7b2eaaaa4ad009dfca372b19d97feef2776 Mon Sep 17 00:00:00 2001 From: James Shubin Date: Wed, 19 Jul 2023 18:53:20 -0400 Subject: [PATCH] pgraph: Make vertex sorting more deterministic If two vertices have the same String output, then sort by pointer string instead. This makes display of certain graphs more deterministic. --- pgraph/pgraph.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/pgraph/pgraph.go b/pgraph/pgraph.go index ce8a71cd..d882ca6e 100644 --- a/pgraph/pgraph.go +++ b/pgraph/pgraph.go @@ -347,7 +347,14 @@ func (vs VertexSlice) Len() int { return len(vs) } func (vs VertexSlice) Swap(i, j int) { vs[i], vs[j] = vs[j], vs[i] } // Less returns the smaller element in the sort order. -func (vs VertexSlice) Less(i, j int) bool { return vs[i].String() < vs[j].String() } +func (vs VertexSlice) Less(i, j int) bool { + a := vs[i].String() + b := vs[j].String() + if a == b { // fallback to ptr compare + return fmt.Sprintf("%p", vs[i]) < fmt.Sprintf("%p", vs[j]) + } + return a < b +} // Sort is a convenience method. func (vs VertexSlice) Sort() { sort.Sort(vs) }