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
", c) // returns 'dafh'
fmt.Printf("Desired result: %s
", 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!