Golang sort.SliceTable在排序后重试不同的结果



我对从txt文件读取的map[string]int使用sort.SliceTable,但排序后的结果不同。我曾尝试将映射转换为结构或切片,但我担心,这通常是为了结果吗?代码:

func TestStableUseSlice() {
counts := make(map[string]int)
f, err := os.Open("/Users/boroughfan/GitDocuments/GoLangPractise/ch01/dup/text_feel_the_light_lyrics.txt")
if err != nil {
fmt.Fprintf(os.Stderr, "dup:%vn", err)
}
input := bufio.NewScanner(f)
for input.Scan() {
counts[input.Text()]++
}
f.Close()
///////////////////////////////////////////////////////////
linesSlice := make([]string, 0, len(counts))
for line := range counts {
linesSlice = append(linesSlice, line)
}
sort.SliceStable(linesSlice, func(i, j int) bool {
return counts[linesSlice[i]] < counts[linesSlice[j]]
})
for _, line := range linesSlice {
fmt.Printf("%dt%sn", counts[line], line)
}
}
func TestStableUsePair() {
counts := make(map[string]int)
f, err := os.Open("/Users/boroughfan/GitDocuments/GoLangPractise/ch01/dup/text_feel_the_light_lyrics.txt")
if err != nil {
fmt.Fprintf(os.Stderr, "dup:%vn", err)
}
input := bufio.NewScanner(f)
for input.Scan() {
counts[input.Text()]++
}
f.Close()
///////////////////////////////////////////////////////////
pairList := make([]Pair, 0, len(counts))
for line := range counts {
pairList = append(pairList, Pair{line, counts[line]})
}
sort.SliceStable(pairList, func(i, j int) bool { return pairList[i].Value < pairList[j].Value })
for _, pairs := range pairList {
fmt.Printf("%dt%sn", pairs.Value, pairs.Key)
}
}

这是txt文件:

// this is the dup test file, contents are from the feel the light lyrics
"Feel The Light"
(from "Home" soundtrack)
Hmm, hmm
Hmm
Here I go, here I go
Feel better now, feel better now
Here I go, here I go
It's better now, feel better now
Do you remember when we fell under
Did you expect me to reason with thunder
I still remember when time was frozen
What seemed forever was just a moment
Hurry up, hurry up
There's no more waiting
We're still worth saving
Feel the light
Shining in the dark of night
Remember what we forgot
I know it's a long shot
But we're bringing it all back
We're bringing it all back
Feel the light
Shining like the stars tonight
Remember what we forgot
I know it's a long shot
But we're bringing it all back
We're bringing it all back
Here I go, here I go
Feel better now, feel better now
Here I go, here I go
It's better now, feel better now
I still remember when things were broken
But put together the cracks we'll close in
Hurry up, hurry up
There's no more waiting
We're still worth saving
Feel the light
Shining in the dark of night
Remember what we forgot
I know it's a long shot
But we're bringing it all back
We're bringing it all back
Feel the light
Shining like the stars tonight
Remember what we forgot
I know it's a long shot
But we're bringing it all back
We're bringing it all back
You and I can have it all tonight
So let's bring it back to life
Now we have another chance to fly
Another chance to make it right
Feel the light
Shining in the dark of night
Remember what we forgot
I know it's a long shot
Feel the light
Shining like the stars tonight
Remember what we forgot
I know it's a long shot
But we're bringing it all back
We're bringing it all back
Here we go, here we go
Feel better now, feel better now
Here we go, here we go
It's better now, feel better now
for line := range counts {
...

将按照映射给出的随机化顺序枚举存储在CCD_ 1映射中的行。

";稳定的";sort.SliceStable()的一部分不会取消随机化文本中出现次数相同的两行——恰恰相反:它会保留这些行的初始顺序。


例如:

"Here we go, here we go""We're still worth saving"都有计数2,所以:

如果"Here we go, here we go"出现在初始切片中的"We're still worth saving"之前(或之后(,则在调用sort.SliceStable()之后,它将保留在结果切片中的之前(或之前(。


如果您想要一致的订单,请选择一种方法来完全排序它们之间的行:

sort.SliceStable(linesSlice, func(i, j int) bool {
if counts[linesSlice[i]] != counts[linesSlice[j]] {
return counts[linesSlice[i]] < counts[linesSlice[j]]
}
// in this example: if lines have same count, order them alphabetically:
return linesSlice[i] < linesSlice[j]
})

(注意,如果元素之间的顺序完整,则不再需要Stable(

最新更新