From 3f6e11d26e96535f33f547b266a744abd166d12d Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sat, 10 Jun 2017 22:55:12 +0200 Subject: [PATCH] Allow sorting nodes in trees --- internal/restic/node.go | 7 +++++++ internal/restic/tree.go | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/internal/restic/node.go b/internal/restic/node.go index 28b9dfedf..73518bf8e 100644 --- a/internal/restic/node.go +++ b/internal/restic/node.go @@ -53,6 +53,13 @@ type Node struct { Path string `json:"-"` } +// Nodes is a slice of nodes that can be sorted. +type Nodes []*Node + +func (n Nodes) Len() int { return len(n) } +func (n Nodes) Less(i, j int) bool { return n[i].Name < n[j].Name } +func (n Nodes) Swap(i, j int) { n[i], n[j] = n[j], n[i] } + func (node Node) String() string { switch node.Type { case "file": diff --git a/internal/restic/tree.go b/internal/restic/tree.go index 2ae5680ec..c2cb3b27b 100644 --- a/internal/restic/tree.go +++ b/internal/restic/tree.go @@ -71,6 +71,13 @@ func (t Tree) binarySearch(name string) (int, *Node, error) { return pos, nil, errors.New("named node not found") } +// Sort sorts the nodes by name. +func (t *Tree) Sort() { + list := Nodes(t.Nodes) + sort.Sort(list) + t.Nodes = list +} + // Subtrees returns a slice of all subtree IDs of the tree. func (t Tree) Subtrees() (trees IDs) { for _, node := range t.Nodes {