Being that NodeArray is nothing more than []*Node that implements the sort.Interface interface
type NodeArray []*Node
Right now, when running, Go panics at copy(nodes[randI:], r.(NodeArray)[:]) either one and tells me that r has type *sort.reverse
What I want to do is to take a slice from an array, reverse it and put it back with the new order, the sort.Interface seemed to allow me to write it in a simple why while proving sort.Swap which helps me in the previous step of swapping nodes
I have no idea how to put the array back in place after reversing because of the types…
sort.Reverse does something different than what you expect. If you see the example in the docs - https://golang.org/pkg/sort/#Reverse - you will see that it is intended to be used with sort.Sort and if you look at the source (https://golang.org/src/sort/sort.go?s=6261:6299#L239) you will see that it basically just reverses the inputs for any calls to the Less method of the sort interface. Its intended purpose is to take a type that you might normally sort in increasing order and instead give you an easy way to sort it in decreasing order.
What you want to do is take an existing array (or slice, I’m not sure which you are actually working with since your code is incomplete) and reverse all the elements in it. To answer this question, I’d first try to answer the question - how would you reverse the order of an integer slice?
That is, given this code:
package main
import (
"fmt"
)
func main() {
numbers := []int{2, 7, 1, 3, 6}
numbers = reverse(numbers)
fmt.Println(numbers)
}
func reverse(slice []int) []int {
var ret []int
// TODO: Implement this
return ret
}
How would you go about implementing the reverse function?
Your solution here will end up being VERY similar to what you likely want in your given example code, but this way we can all work on the code together and it is much simpler since everyone understands an integer slice pretty well.
I’d suggest taking a stab at implementing that code first. If you have questions let me know where specifically you are getting stuck. eg are you having issues swapping two elements? Are you unsure of which elements to swap? Are you unsure of the overall algorithm needed to reverse a slice?