Skip to content

Commit

Permalink
Merge pull request #124 from YoonAHMM/bugfix/golang-sortMap-issues
Browse files Browse the repository at this point in the history
修复排序bug
  • Loading branch information
toolgood authored Aug 31, 2024
2 parents 3ef8032 + afd5586 commit 3e54f92
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions golang/ToolGood/Words/internals/BaseSearchEx.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,20 +282,28 @@ func (this *BaseSearchEx)CreateDict(keywords []string) int {
}
return len(dictionary)
}
func (s *BaseSearchEx)sortMap(mp map[int32]int) []int32 {
var newMp = make([]int, 0)
var newMpKey = make([]int32, 0)
for oldk, v := range mp {
newMp = append(newMp, v)
newMpKey = append(newMpKey, oldk)
}
sort.Ints(newMp)
func (s *BaseSearchEx) sortMap(mp map[int32]int) []int32 {

type kv struct {
Key int32
Value int
}
var sortedPairs []kv
for k, v := range mp {
sortedPairs = append(sortedPairs, kv{k, v})
}

list:=make([]int32, 0)
for k, _ := range newMp {
list = append(list, newMpKey[k])
}
return list

sort.Slice(sortedPairs, func(i, j int) bool {
return sortedPairs[i].Value < sortedPairs[j].Value
})


var sortedKeys []int32
for _, pair := range sortedPairs {
sortedKeys = append(sortedKeys, pair.Key)
}

return sortedKeys
}


0 comments on commit 3e54f92

Please sign in to comment.