pgraph: Add sort function to sort a list of vertices

With tests too!
This commit is contained in:
James Shubin
2017-05-31 15:32:38 -04:00
parent 458e115490
commit bd4563b699
2 changed files with 60 additions and 0 deletions

View File

@@ -623,3 +623,15 @@ func Reverse(vs []Vertex) []Vertex {
}
return out
}
// Sort the list of vertices and return a copy without modifying the input.
func Sort(vs []Vertex) []Vertex {
vertices := []Vertex{}
for _, v := range vs { // copy
vertices = append(vertices, v)
}
sort.Sort(VertexSlice(vertices))
return vertices
// sort.Sort(VertexSlice(vs)) // this is wrong, it would modify input!
//return vs
}