diff --git a/golang/ToolGood/Words/internals/BaseSearchEx.go b/golang/ToolGood/Words/internals/BaseSearchEx.go index 69ce7f4..6db33d2 100644 --- a/golang/ToolGood/Words/internals/BaseSearchEx.go +++ b/golang/ToolGood/Words/internals/BaseSearchEx.go @@ -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 } - \ No newline at end of file