Sort slice with respect to another slice in go

huangapple go评论90阅读模式
英文:

Sort slice with respect to another slice in go

问题

我正在尝试找出一种方法,以使一个切片相对于另一个切片进行排序,例如:

我想相对于other_slicemain_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 ( &quot;fmt&quot;
	     &quot;sort&quot;
)

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] &lt; 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(&quot;Not sorted : &quot;, my_two_slices.main_slice)

	sort.Sort(SortByOther(my_two_slices))
	fmt.Println(&quot;Sorted : &quot;, 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]
}

huangapple
  • 本文由 发表于 2015年1月2日 02:32:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/27733469.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定