英文:
Sort slice with respect to another slice in go
问题
我正在尝试找出一种方法,以使一个切片相对于另一个切片进行排序,例如:
我想相对于other_slice
对main_slice
进行排序
other_slice = []int{3,5,1,2,7}
main_slice = []int{1,2,3,4,5}
在main_slice
中,3
对应于other_slice
中的最小值(1
),4
对应于第二小的值(2
);因此,我期望排序后的main_slice
为:{3,4,1,2,5}
我参考了这个教程,但是无法找到解决方案,以下是我的尝试:
package main
import (
"fmt"
"sort"
)
type TwoSlices struct {
main_slice []int
other_slice []int
}
type SortByOther TwoSlices
func (sbo SortByOther) Len() int {
return len(sbo.main_slice)
}
func (sbo SortByOther) Swap(i, j int) {
sbo.main_slice[i], sbo.main_slice[j] = sbo.main_slice[j], sbo.main_slice[i]
}
func (sbo SortByOther) Less(i, j int) bool {
return sbo.other_slice[i] < sbo.other_slice[j]
}
func main() {
my_other_slice := []int{3, 5, 1, 2, 7}
my_main_slice := []int{1, 2, 3, 4, 5} // sorted : {3,4,1,2,5}
my_two_slices := TwoSlices{main_slice: my_main_slice, other_slice: my_other_slice}
fmt.Println("Not sorted:", my_two_slices.main_slice)
sort.Sort(SortByOther(my_two_slices))
fmt.Println("Sorted:", my_two_slices.main_slice)
}
我的输出结果:
Not sorted: [1 2 3 4 5]
Sorted: [1 3 2 4 5]
main_slice
的值发生了变化,但它并没有达到我想要的效果,我做错了什么?
英文:
I am trying to figure out a way to sort one slice with respect to the other, for example:
I want to sort main_slice
with respect to other_slice
other_slice = []int{3,5,1,2,7}
main_slice = []int{1,2,3,4,5}
3
in main_slice
corresponds to lowest value in other_slice
(1
), 4
to the second lowest (2
); therefore, I expect sorted main_slice to be
: {3,4,1,2,5}
I used this tutorial as reference, but could not come up with a solution, here is my try:
package main
import ( "fmt"
"sort"
)
type TwoSlices struct {
main_slice []int
other_slice []int
}
type SortByOther TwoSlices
func (sbo SortByOther) Len() int {
return len(sbo.main_slice)
}
func (sbo SortByOther) Swap(i, j int) {
sbo.main_slice[i], sbo.main_slice[j] = sbo.main_slice[j], sbo.main_slice[i]
}
func (sbo SortByOther) Less(i, j int) bool {
return sbo.other_slice[i] < sbo.other_slice[j]
}
func main() {
my_other_slice := []int{3,5,1,2,7}
my_main_slice := []int{1,2,3,4,5} // sorted : {3,4,1,2,5}
my_two_slices := TwoSlices{main_slice: my_main_slice, other_slice: my_other_slice}
fmt.Println("Not sorted : ", my_two_slices.main_slice)
sort.Sort(SortByOther(my_two_slices))
fmt.Println("Sorted : ", my_two_slices.main_slice)
}
My output:
Not sorted : [1 2 3 4 5]
Sorted : [1 3 2 4 5]
main_slice
is getting changed, but it does not do what I want, what am I doing wrong?
答案1
得分: 6
你在Swap
的实现中忘记了交换other_slice
的元素:
func (sbo SortByOther) Swap(i, j int) {
sbo.main_slice[i], sbo.main_slice[j] = sbo.main_slice[j], sbo.main_slice[i]
sbo.other_slice[i], sbo.other_slice[j] = sbo.other_slice[j], sbo.other_slice[i]
}
英文:
You forgot to swap the elements of other_slice
in the implementation of Swap
:
func (sbo SortByOther) Swap(i, j int) {
sbo.main_slice[i], sbo.main_slice[j] = sbo.main_slice[j], sbo.main_slice[i]
sbo.other_slice[i], sbo.other_slice[j] = sbo.other_slice[j], sbo.other_slice[i]
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论