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?