From 4418296a93febdcf04712d49083bede322ea8223 Mon Sep 17 00:00:00 2001 From: Sander van Harmelen Date: Thu, 21 Nov 2024 14:53:19 +0100 Subject: [PATCH 1/2] Make the items in the tree alphabetically ordered --- components/Tree.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/components/Tree.go b/components/Tree.go index 17ac075..f8aa64b 100644 --- a/components/Tree.go +++ b/components/Tree.go @@ -2,6 +2,7 @@ package components import ( "fmt" + "sort" "strings" "github.com/gdamore/tcell/v2" @@ -291,7 +292,20 @@ func NewTree(dbName string, dbdriver drivers.Driver) *Tree { func (tree *Tree) databasesToNodes(children map[string][]string, node *tview.TreeNode, defaultExpanded bool) { node.ClearChildren() - for key, values := range children { + // Sort the keys and use them to loop over the + // children so they are always in the same order. + sortedKeys := make([]string, 0, len(children)) + for key := range children { + sortedKeys = append(sortedKeys, key) + } + sort.Strings(sortedKeys) + + for _, key := range sortedKeys { + values := children[key] + + // Sort the values. + sort.Strings(values) + var rootNode *tview.TreeNode nodeReference := node.GetReference().(string) From 4bb8bc3e6c9b3c8a0ab79a31f512c12813ed0790 Mon Sep 17 00:00:00 2001 From: Sander van Harmelen Date: Fri, 22 Nov 2024 11:46:44 +0100 Subject: [PATCH 2/2] Improved the sorting solution by updating to Go 1.23 --- components/Tree.go | 8 +++----- go.mod | 2 +- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/components/Tree.go b/components/Tree.go index f8aa64b..02231b1 100644 --- a/components/Tree.go +++ b/components/Tree.go @@ -2,6 +2,8 @@ package components import ( "fmt" + "maps" + "slices" "sort" "strings" @@ -294,11 +296,7 @@ func (tree *Tree) databasesToNodes(children map[string][]string, node *tview.Tre // Sort the keys and use them to loop over the // children so they are always in the same order. - sortedKeys := make([]string, 0, len(children)) - for key := range children { - sortedKeys = append(sortedKeys, key) - } - sort.Strings(sortedKeys) + sortedKeys := slices.Sorted(maps.Keys(children)) for _, key := range sortedKeys { values := children[key] diff --git a/go.mod b/go.mod index 140d36a..7587174 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/jorgerojas26/lazysql -go 1.20 +go 1.23 require ( github.com/DATA-DOG/go-sqlmock v1.5.2