From ed6401955567ec1da563d907348df513b68b0b31 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Thu, 28 Apr 2022 22:33:30 +0200 Subject: [PATCH] Order output --- main.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 54659ed..d377f05 100644 --- a/main.go +++ b/main.go @@ -166,6 +166,17 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return m, tea.Batch(cmds...) } +func InsertWord(arr []word, w word) []word { + if len(arr) == 0 { + return append(arr, w) + } + w2 := arr[len(arr)-1] + if w.distance < w2.distance { + return append(arr, w) + } + return append(InsertWord(arr[:len(arr)-1], w), w2) +} + func (m model) InputWord() (model, tea.Cmd) { input := m.textInput.Value() @@ -174,9 +185,8 @@ func (m model) InputWord() (model, tea.Cmd) { m.maxLength = len(input) } // TODO: Get distance and ranking from a file - // TODO: Reorder the array w := word{input, 200*rand.Float64() - 100, rand.Intn(1000)} - m.words = append(m.words, w) + m.words = InsertWord(m.words, w) lineWidth := lipgloss.Width(fmt.Sprintf( "* %s %s %s %4d ",