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.
This commit is contained in:
James Shubin
2023-07-19 18:53:20 -04:00
parent 5e0922395c
commit 6a06f7b2ea

View File

@@ -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) }