英文:
Sort struct by order of string with same values
问题
我正在尝试按照另一个字符串的字符顺序对一组结构体进行排序,这些结构体具有相同的值。
这是一个示例:
package main
import (
"fmt"
"sort"
"strings"
)
type Container struct {
Initial string
}
func main() {
s := "dfah"
c := []Container{}
for _, r := range "fadh" {
c = append(c, Container{Initial: string(r)})
}
sort.Slice(c, func(i, j int) bool {
str := strings.Compare(c[i].Initial, s)
if str == -1 {
return true
} else {
return false
}
})
fmt.Printf("Result: %s\n", c) // 返回 'dafh'
fmt.Printf("Desired result: %s\n", s) // 返回 'dfah'
}
期望的结果是按照字符串 'dfah' 的顺序对 Container
结构体进行排序。
它们始终具有相同的字符/字符数量,只是未排序。我不确定实现这一目标的正确方法是什么。有什么想法吗?谢谢!
英文:
I'm trying to sort a collection of structs by the order of another strings characters with the same values.
Here's an example:
package main
import (
"fmt"
"sort"
"strings"
)
type Container struct {
Initial string
}
func main() {
s := "dfah"
c := []Container{}
for _, r := range "fadh" {
c = append(c, Container{Initial: string(r)})
}
sort.Slice(c, func(i, j int) bool {
str := strings.Compare(c[i].Initial, s)
if str == -1 {
return true
} else {
return false
}
})
fmt.Printf("Result: %s\n", c) // returns 'dafh'
fmt.Printf("Desired result: %s\n", s) // returns 'dfah'
}
The desired result would be the sorted collection of Container
structs, with the same order of the 'dfah' string.
https://play.golang.org/p/eDW5-xpCzv
They will always have the same characters/number of characters, just unsorted. I'm not sure what the correct way to accomplish this is. Any ideas? Thanks!
答案1
得分: 1
在你的切片排序函数中,你将c[i].Initial
与s
进行比较。这是错误的。相反,你想要找出c[i].Initial
在s
中是在c[j].Initial
之前还是之后。
以下是一些示例代码,或者你可以在playground上运行它:
package main
import (
"fmt"
"sort"
"strings"
)
type Container struct {
Initial string
}
func main() {
s := "dfah"
c := []Container{}
for _, r := range "fadh" {
c = append(c, Container{Initial: string(r)})
}
sort.Slice(c, func(i, j int) bool {
return strings.Index(s, c[i].Initial) <= strings.Index(s, c[j].Initial)
})
fmt.Printf("Result: %s\n", c) // 返回 [{d} {f} {a} {h}]
fmt.Printf("Desired result: %s\n", s) // 返回 'dfah'
}
请注意,在实际应用中,这种方法效率低下,因为它需要多次扫描s
。如果这是真实的代码,我会构建一个从s
的字符到它们的索引的映射,这样就可以用映射查找来替代strings.Index
调用。
英文:
In your slice-sorting function, you're comparing c[i].Initial
to s
. That's simply wrong. Instead, you want to find whether c[i].Initial
appears in s
before or after c[j].Initial
.
Here's some example code, or see it running on the playground:
package main
import (
"fmt"
"sort"
"strings"
)
type Container struct {
Initial string
}
func main() {
s := "dfah"
c := []Container{}
for _, r := range "fadh" {
c = append(c, Container{Initial: string(r)})
}
sort.Slice(c, func(i, j int) bool {
return strings.Index(s, c[i].Initial) <= strings.Index(s, c[j].Initial)
})
fmt.Printf("Result: %s\n", c) // returns [{d} {f} {a} {h}]
fmt.Printf("Desired result: %s\n", s) // returns 'dfah'
}
Note in practice, this is inefficient, since it involves scanning s
lots of times. If this were real code, I'd be building a map from characters of s
to their index so that the strings.Index
calls could be replaced by map lookups.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论