Skip to content

Commit

Permalink
⚡ Sort levels based on numerical id value
Browse files Browse the repository at this point in the history
  • Loading branch information
beskay committed Mar 15, 2024
1 parent 80bf845 commit 31b596e
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions internal/tui/listLevels.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tui
import (
"fmt"
"sort"
"strconv"
"strings"

tea "github.com/charmbracelet/bubbletea"
Expand All @@ -25,9 +26,21 @@ func (m *levelListModel) Init() tea.Cmd {
m.Keys = append(m.Keys, k)
}

// Sort the keys based on the ID field in the utils.Level struct
// Sort the keys based on the numerical value of the ID field in the utils.Level struct
sort.Slice(m.Keys, func(i, j int) bool {
return m.Levels[m.Keys[i]].ID < m.Levels[m.Keys[j]].ID
idI, errI := strconv.Atoi(m.Levels[m.Keys[i]].ID)
if errI != nil {
fmt.Printf("Error converting level ID '%s' to integer: %v\n", m.Levels[m.Keys[i]].ID, errI)
// Consider how you want to handle this error. For simplicity, this example just prints an error message.
}

idJ, errJ := strconv.Atoi(m.Levels[m.Keys[j]].ID)
if errJ != nil {
fmt.Printf("Error converting level ID '%s' to integer: %v\n", m.Levels[m.Keys[j]].ID, errJ)
// Consider how you want to handle this error. For simplicity, this example just prints an error message.
}

return idI < idJ
})

return nil
Expand Down

0 comments on commit 31b596e

Please sign in to comment.